def create_socket(conf): """ Create a new socket for the given address. If the address is a tuple, a TCP socket is created. If it is a string, a Unix socket is created. Otherwise a TypeError is raised. """ # get it only once addr = util.parse_address(conf.get('address', ('127.0.0.1', 8000))) if isinstance(addr, tuple): if util.is_ipv6(addr[0]): sock_type = TCP6Socket else: sock_type = TCPSocket elif isinstance(addr, basestring): sock_type = UnixSocket else: raise TypeError("Unable to create socket from: %r" % addr) if 'PISTIL_FD' in os.environ: fd = int(os.environ.pop('PISTIL_FD')) try: return sock_type(addr, conf, fd=fd) except socket.error, e: if e[0] == errno.ENOTCONN: log.error("PISTIL_FD should refer to an open socket.") else: raise
def on_init(self, args): self.address = util.parse_address( args.get('address', ('127.0.0.1', 8000))) if not self._LISTENER: self._LISTENER = create_socket(args) # we want to pass the socket to the worker. self.conf.update({"sock": self._LISTENER})
def on_init(self, args): self.address = util.parse_address(args.get('address', ('127.0.0.1', 8000))) if not self._LISTENER: self._LISTENER = create_socket(args) # we want to pass the socket to the worker. self.conf.update({"sock": self._LISTENER})
def __init__(self, conf, fd=None): self.conf = conf self.address = util.parse_address(conf.get('address', ('127.0.0.1', 8000))) if fd is None: sock = socket.socket(self.FAMILY, socket.SOCK_STREAM) else: sock = socket.fromfd(fd, self.FAMILY, socket.SOCK_STREAM) self.sock = self.set_options(sock, bound=(fd is not None))
def __init__(self, conf, fd=None): self.conf = conf self.address = util.parse_address( conf.get('address', ('127.0.0.1', 8000))) if fd is None: sock = socket.socket(self.FAMILY, socket.SOCK_STREAM) else: sock = socket.fromfd(fd, self.FAMILY, socket.SOCK_STREAM) self.sock = self.set_options(sock, bound=(fd is not None))
def run_server(): parser = argparse.ArgumentParser( description='serve a static file folder') parser.add_argument('path', type=str, help='Folder to serve', default=".", nargs='?') parser.add_argument( '--bind', type=str, default="127.0.0.1:5000", help="""\ The socket to bind. A string of the form: 'HOST', 'HOST:PORT', 'unix:PATH'. An IP is a valid HOST. """) parser.add_argument( '--workers', type=int, default=1, help='number of workers') parser.add_argument( '--name', type=str, default="fserve", help='name of the server') parser.add_argument( '--debug', action='store_true', default=False, help='Debug mode') args = parser.parse_args() path = args.path if path == ".": path = os.getcwd() address = parse_address(util.to_bytestring(args.bind)) conf = {"address": address, "debug": args.debug, "num_workers": args.workers} spec = (HttpWorker, 30, "worker", {"path": path}, args.name,) arbiter = TcpArbiter(conf, spec, name=args.name) arbiter.run()
def __init__(self, address, serialize, unserialize): self.address = parse_address(address) self.serialize = serialize self.unserialize = unserialize