Changeset 45:ca72a5901070
- Timestamp:
- 11/24/06 18:00:02
(2 years ago)
- Author:
- Allan Saddi <allan@saddi.com>
- branch:
- default
- convert_revision:
- svn:46762da8-4eb7-0310-94e9-d918b60927c8/flup/trunk@2111
- Message:
Experimental support for SCGI over UNIX domain sockets.
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r44 |
r45 |
|
| 137 | 137 | |
|---|
| 138 | 138 | def _getConnection(self): |
|---|
| 139 | | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|---|
| | 139 | if type(self._connect) is str: |
|---|
| | 140 | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
|---|
| | 141 | else: |
|---|
| | 142 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|---|
| 140 | 143 | sock.connect(self._connect) |
|---|
| 141 | 144 | return sock |
|---|
| r32 |
r45 |
|
| 90 | 90 | def __init__(self, application, scriptName='', environ=None, |
|---|
| 91 | 91 | multithreaded=True, multiprocess=False, |
|---|
| 92 | | bindAddress=('localhost', 4000), allowedServers=None, |
|---|
| | 92 | bindAddress=('localhost', 4000), umask=None, |
|---|
| | 93 | allowedServers=None, |
|---|
| 93 | 94 | loggingLevel=logging.INFO, debug=True, **kw): |
|---|
| 94 | 95 | """ |
|---|
| … | … | |
| 101 | 102 | environment variables you want to pass to your application. |
|---|
| 102 | 103 | |
|---|
| 103 | | bindAddress is the address to bind to, which must be a tuple of |
|---|
| 104 | | length 2. The first element is a string, which is the host name |
|---|
| 105 | | or IPv4 address of a local interface. The 2nd element is the port |
|---|
| 106 | | number. |
|---|
| | 104 | bindAddress is the address to bind to, which must be a string or |
|---|
| | 105 | a tuple of length 2. If a tuple, the first element must be a string, |
|---|
| | 106 | which is the host name or IPv4 address of a local interface. The |
|---|
| | 107 | 2nd element of the tuple is the port number. If a string, it will |
|---|
| | 108 | be interpreted as a filename and a UNIX socket will be opened. |
|---|
| 107 | 109 | |
|---|
| | 110 | If binding to a UNIX socket, umask may be set to specify what |
|---|
| | 111 | the umask is to be changed to before the socket is created in the |
|---|
| | 112 | filesystem. After the socket is created, the previous umask is |
|---|
| | 113 | restored. |
|---|
| | 114 | |
|---|
| 108 | 115 | allowedServers must be None or a list of strings representing the |
|---|
| 109 | 116 | IPv4 addresses of servers allowed to connect. None means accept |
|---|
| … | … | |
| 118 | 125 | multiprocess=multiprocess, |
|---|
| 119 | 126 | bindAddress=bindAddress, |
|---|
| | 127 | umask=umask, |
|---|
| 120 | 128 | allowedServers=allowedServers, |
|---|
| 121 | 129 | loggingLevel=loggingLevel, |
|---|
| r37 |
r45 |
|
| 36 | 36 | import signal |
|---|
| 37 | 37 | import datetime |
|---|
| | 38 | import os |
|---|
| 38 | 39 | |
|---|
| 39 | 40 | # Threads are required. If you want a non-threaded (forking) version, look at |
|---|
| … | … | |
| 273 | 274 | def __init__(self, application, scriptName='', environ=None, |
|---|
| 274 | 275 | multithreaded=True, multiprocess=False, |
|---|
| 275 | | bindAddress=('localhost', 4000), allowedServers=NoDefault, |
|---|
| | 276 | bindAddress=('localhost', 4000), umask=None, |
|---|
| | 277 | allowedServers=NoDefault, |
|---|
| 276 | 278 | loggingLevel=logging.INFO, debug=True): |
|---|
| 277 | 279 | """ |
|---|
| … | … | |
| 289 | 291 | True. (Only makes sense with threaded servers.) |
|---|
| 290 | 292 | |
|---|
| 291 | | bindAddress is the address to bind to, which must be a tuple of |
|---|
| 292 | | length 2. The first element is a string, which is the host name |
|---|
| 293 | | or IPv4 address of a local interface. The 2nd element is the port |
|---|
| 294 | | number. |
|---|
| 295 | | |
|---|
| | 293 | bindAddress is the address to bind to, which must be a string or |
|---|
| | 294 | a tuple of length 2. If a tuple, the first element must be a string, |
|---|
| | 295 | which is the host name or IPv4 address of a local interface. The |
|---|
| | 296 | 2nd element of the tuple is the port number. If a string, it will |
|---|
| | 297 | be interpreted as a filename and a UNIX socket will be opened. |
|---|
| | 298 | |
|---|
| | 299 | If binding to a UNIX socket, umask may be set to specify what |
|---|
| | 300 | the umask is to be changed to before the socket is created in the |
|---|
| | 301 | filesystem. After the socket is created, the previous umask is |
|---|
| | 302 | restored. |
|---|
| | 303 | |
|---|
| 296 | 304 | allowedServers must be None or a list of strings representing the |
|---|
| 297 | 305 | IPv4 addresses of servers allowed to connect. None means accept |
|---|
| … | … | |
| 311 | 319 | self.debug = debug |
|---|
| 312 | 320 | self._bindAddress = bindAddress |
|---|
| | 321 | self._umask = umask |
|---|
| 313 | 322 | if allowedServers is NoDefault: |
|---|
| 314 | 323 | allowedServers = ['127.0.0.1'] |
|---|
| … | … | |
| 323 | 332 | def _setupSocket(self): |
|---|
| 324 | 333 | """Creates and binds the socket for communication with the server.""" |
|---|
| 325 | | sock = socket.socket() |
|---|
| 326 | | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
|---|
| | 334 | oldUmask = None |
|---|
| | 335 | if type(self._bindAddress) is str: |
|---|
| | 336 | # Unix socket |
|---|
| | 337 | sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
|---|
| | 338 | try: |
|---|
| | 339 | os.unlink(self._bindAddress) |
|---|
| | 340 | except OSError: |
|---|
| | 341 | pass |
|---|
| | 342 | if self._umask is not None: |
|---|
| | 343 | oldUmask = os.umask(self._umask) |
|---|
| | 344 | else: |
|---|
| | 345 | # INET socket |
|---|
| | 346 | assert type(self._bindAddress) is tuple |
|---|
| | 347 | assert len(self._bindAddress) == 2 |
|---|
| | 348 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|---|
| | 349 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
|---|
| | 350 | |
|---|
| 327 | 351 | sock.bind(self._bindAddress) |
|---|
| 328 | 352 | sock.listen(socket.SOMAXCONN) |
|---|
| | 353 | |
|---|
| | 354 | if oldUmask is not None: |
|---|
| | 355 | os.umask(oldUmask) |
|---|
| | 356 | |
|---|
| 329 | 357 | return sock |
|---|
| 330 | 358 | |
|---|
| … | … | |
| 334 | 362 | |
|---|
| 335 | 363 | def _isClientAllowed(self, addr): |
|---|
| 336 | | ret = self._allowedServers is None or addr[0] in self._allowedServers |
|---|
| | 364 | ret = self._allowedServers is None or \ |
|---|
| | 365 | len(addr) != 2 or \ |
|---|
| | 366 | (len(addr) == 2 and addr[0] in self._allowedServers) |
|---|
| 337 | 367 | if not ret: |
|---|
| 338 | 368 | self.logger.warning('Server connection from %s disallowed', |
|---|
| r32 |
r45 |
|
| 89 | 89 | """ |
|---|
| 90 | 90 | def __init__(self, application, scriptName='', environ=None, |
|---|
| 91 | | bindAddress=('localhost', 4000), allowedServers=None, |
|---|
| | 91 | bindAddress=('localhost', 4000), umask=None, |
|---|
| | 92 | allowedServers=None, |
|---|
| 92 | 93 | loggingLevel=logging.INFO, debug=True, **kw): |
|---|
| 93 | 94 | """ |
|---|
| … | … | |
| 100 | 101 | environment variables you want to pass to your application. |
|---|
| 101 | 102 | |
|---|
| 102 | | bindAddress is the address to bind to, which must be a tuple of |
|---|
| 103 | | length 2. The first element is a string, which is the host name |
|---|
| 104 | | or IPv4 address of a local interface. The 2nd element is the port |
|---|
| 105 | | number. |
|---|
| | 103 | bindAddress is the address to bind to, which must be a string or |
|---|
| | 104 | a tuple of length 2. If a tuple, the first element must be a string, |
|---|
| | 105 | which is the host name or IPv4 address of a local interface. The |
|---|
| | 106 | 2nd element of the tuple is the port number. If a string, it will |
|---|
| | 107 | be interpreted as a filename and a UNIX socket will be opened. |
|---|
| 106 | 108 | |
|---|
| | 109 | If binding to a UNIX socket, umask may be set to specify what |
|---|
| | 110 | the umask is to be changed to before the socket is created in the |
|---|
| | 111 | filesystem. After the socket is created, the previous umask is |
|---|
| | 112 | restored. |
|---|
| | 113 | |
|---|
| 107 | 114 | allowedServers must be None or a list of strings representing the |
|---|
| 108 | 115 | IPv4 addresses of servers allowed to connect. None means accept |
|---|
| … | … | |
| 117 | 124 | multiprocess=True, |
|---|
| 118 | 125 | bindAddress=bindAddress, |
|---|
| | 126 | umask=umask, |
|---|
| 119 | 127 | allowedServers=allowedServers, |
|---|
| 120 | 128 | loggingLevel=loggingLevel, |
|---|