MCPcopy
hub / github.com/benoitc/gunicorn / create_sockets

Function create_sockets

gunicorn/sock.py:165–227  ·  view source on GitHub ↗

Create a new socket for the configured addresses or file descriptors. If a configured address is a tuple then a TCP socket is created. If it is a string, a Unix socket is created. Otherwise, a TypeError is raised.

(conf, log, fds=None)

Source from the content-addressed store, hash-verified

163
164
165def create_sockets(conf, log, fds=None):
166 """
167 Create a new socket for the configured addresses or file descriptors.
168
169 If a configured address is a tuple then a TCP socket is created.
170 If it is a string, a Unix socket is created. Otherwise, a TypeError is
171 raised.
172 """
173 listeners = []
174
175 # get it only once
176 addr = conf.address
177 fdaddr = [bind for bind in addr if isinstance(bind, int)]
178 if fds:
179 fdaddr += list(fds)
180 laddr = [bind for bind in addr if not isinstance(bind, int)]
181
182 # check ssl config early to raise the error on startup
183 # only the certfile is needed since it can contains the keyfile
184 if conf.certfile and not os.path.exists(conf.certfile):
185 raise ValueError('certfile "%s" does not exist' % conf.certfile)
186
187 if conf.keyfile and not os.path.exists(conf.keyfile):
188 raise ValueError('keyfile "%s" does not exist' % conf.keyfile)
189
190 # sockets are already bound
191 if fdaddr:
192 for fd in fdaddr:
193 sock = socket.fromfd(fd, socket.AF_UNIX, socket.SOCK_STREAM)
194 sock_name = sock.getsockname()
195 sock_type = _sock_type(sock_name)
196 listener = sock_type(sock_name, conf, log, fd=fd)
197 listeners.append(listener)
198
199 return listeners
200
201 # no sockets is bound, first initialization of gunicorn in this env.
202 for addr in laddr:
203 sock_type = _sock_type(addr)
204 sock = None
205 for i in range(5):
206 try:
207 sock = sock_type(addr, conf, log)
208 except OSError as e:
209 if e.args[0] == errno.EADDRINUSE:
210 log.error("Connection in use: %s", str(addr))
211 if e.args[0] == errno.EADDRNOTAVAIL:
212 log.error("Invalid address: %s", str(addr))
213 msg = "connection to {addr} failed: {error}"
214 log.error(msg.format(addr=str(addr), error=str(e)))
215 if i < 5:
216 log.debug("Retrying in 1 second.")
217 time.sleep(1)
218 else:
219 break
220
221 if sock is None:
222 log.error("Can't connect to %s", str(addr))

Callers

nothing calls this directly

Calls 5

_sock_typeFunction · 0.85
existsMethod · 0.80
getsocknameMethod · 0.45
errorMethod · 0.45
debugMethod · 0.45

Tested by

no test coverage detected