def __init__(self, address='/dev/log'): self.address = address self.stream = 0 if isinstance(address, type('')): try: self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) self.socket.connect(address) except socket.error: # Some Linux installations have /dev/log # a stream socket instead of a datagram socket. self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self.stream = 1 else: self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def __init__ (self, address='/dev/log'): self.address = address self.stream = 0 if isinstance(address, type('')): try: self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) self.socket.connect(address) except socket.error: # Some Linux installations have /dev/log # a stream socket instead of a datagram socket. self.socket = socket.socket (socket.AF_UNIX, socket.SOCK_STREAM) self.stream = 1 else: self.socket = socket.socket (socket.AF_INET, socket.SOCK_DGRAM)
def __init__(self, ip, port, logger_object): self.ip = ip self.port = port sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.prebind(sock, logger_object) self.bind((ip, port)) if not ip: self.log_info('Computing default hostname', 'warning') hostname = socket.gethostname() try: ip = socket.gethostbyname(hostname) except socket.error: raise ValueError( 'Could not determine IP address for hostname %s, ' 'please try setting an explicit IP address in the "port" ' 'setting of your [inet_http_server] section. For example, ' 'instead of "port = 9001", try "port = 127.0.0.1:9001."' % hostname) try: self.server_name = socket.gethostbyaddr (ip)[0] except socket.error: self.log_info('Cannot do reverse lookup', 'warning') self.server_name = ip # use the IP address as the "hostname" self.postbind()
def __init__(self, ip, port, logger_object): self.ip = ip self.port = port sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.prebind(sock, logger_object) self.bind((ip, port)) if not ip: self.log_info('Computing default hostname', 'warning') hostname = socket.gethostname() try: ip = socket.gethostbyname(hostname) except socket.error: raise ValueError( 'Could not determine IP address for hostname %s, ' 'please try setting an explicit IP address in the "port" ' 'setting of your [inet_http_server] section. For example, ' 'instead of "port = 9001", try "port = 127.0.0.1:9001."' % hostname) try: self.server_name = socket.gethostbyaddr (ip)[0] except socket.error: self.log_info('Cannot do reverse lookup', 'warning') self.server_name = ip # use the IP address as the "hostname" self.postbind()
def get (request, host='', port=8080): s = socket.socket (socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) s.send(request) while 1: d = s.recv (8192) if not d: break s.close()
def create_and_bind(self): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(self.addr()) except: sock.close() raise return sock
def get(request, host='', port=8080): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) s.send(request) while 1: d = s.recv(8192) if not d: break s.close()
def create_and_bind(self): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(self.addr()) except: sock.close() raise return sock
def checkused(self, socketname): s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: s.connect(socketname) s.send("GET / HTTP/1.0\r\n\r\n") s.recv(1) s.close() except socket.error: return False else: return True
def bench(host, port=80, n=100): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) t = timer() s.send((request * n) + last_request) while 1: d = s.recv(65536) if not d: break total = t.end() print('time: %.2f seconds (%.2f hits/sec)' % (total, n / total))
def checkused(self, socketname): s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: s.connect(socketname) s.send("GET / HTTP/1.0\r\n\r\n") s.recv(1) s.close() except socket.error: return False else: return True
def max_client_sockets(): # make a server socket server = socket.socket (socket.AF_INET, socket.SOCK_STREAM) server.bind (('', 9999)) server.listen (5) sl = [] while 1: try: s = socket.socket (socket.AF_INET, socket.SOCK_STREAM) s.connect (('', 9999)) conn, addr = server.accept() sl.append ((s,conn)) except: break num = len(sl) for s,c in sl: s.close() c.close() del sl return num
def max_client_sockets(): # make a server socket server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind(('', 9999)) server.listen(5) sl = [] while 1: try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('', 9999)) conn, addr = server.accept() sl.append((s, conn)) except: break num = len(sl) for s, c in sl: s.close() c.close() del sl return num
def bench (host, port=80, n=100): s = socket.socket (socket.AF_INET, socket.SOCK_STREAM) s.connect ((host, port)) t = timer() s.send ((request * n) + last_request) while 1: d = s.recv(65536) if not d: break total = t.end() print('time: %.2f seconds (%.2f hits/sec)' % (total, n / total))
def create_and_bind(self): if os.path.exists(self.path): os.unlink(self.path) sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.bind(self.addr()) try: self._chown() self._chmod() except: sock.close() os.unlink(self.path) raise return sock
def max_server_sockets(): sl = [] while 1: try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('', 0)) s.listen(5) sl.append(s) except: break num = len(sl) for s in sl: s.close() del sl return num
def __init__ (self): a = socket.socket(socket.AF_INET, socket.SOCK_STREAM) w = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # set TCP_NODELAY to true to avoid buffering w.setsockopt(socket.IPPROTO_TCP, 1, 1) # tricky: get a pair of connected sockets for i in range(NPORTS): trigger.portoffset = (trigger.portoffset + 1) % NPORTS port = MINPORT + trigger.portoffset address = (HOST, port) try: a.bind(address) except socket.error: continue else: break else: raise RuntimeError('Cannot bind trigger!') a.listen(1) w.setblocking(0) try: w.connect(address) except: pass r, addr = a.accept() a.close() w.setblocking(1) self.trigger = w asyncore.dispatcher.__init__(self, r) self.lock = thread.allocate_lock() self.thunks = [] self._trigger_connected = 0
def __init__(self): a = socket.socket(socket.AF_INET, socket.SOCK_STREAM) w = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # set TCP_NODELAY to true to avoid buffering w.setsockopt(socket.IPPROTO_TCP, 1, 1) # tricky: get a pair of connected sockets for i in range(NPORTS): trigger.portoffset = (trigger.portoffset + 1) % NPORTS port = MINPORT + trigger.portoffset address = (HOST, port) try: a.bind(address) except socket.error: continue else: break else: raise RuntimeError('Cannot bind trigger!') a.listen(1) w.setblocking(0) try: w.connect(address) except: pass r, addr = a.accept() a.close() w.setblocking(1) self.trigger = w asyncore.dispatcher.__init__(self, r) self.lock = thread.allocate_lock() self.thunks = [] self._trigger_connected = 0
def max_server_sockets(): sl = [] while 1: try: s = socket.socket (socket.AF_INET, socket.SOCK_STREAM) s.bind (('',0)) s.listen(5) sl.append (s) except: break num = len(sl) for s in sl: s.close() del sl return num
def __init__(self, socketname, sockchmod, sockchown, logger_object): self.ip = socketname self.port = socketname # XXX this is insecure. We really should do something like # http://developer.apple.com/samplecode/CFLocalServer/listing6.html # (see also http://developer.apple.com/technotes/tn2005/tn2083.html#SECUNIXDOMAINSOCKETS) # but it would be very inconvenient for the user to need to get all # the directory setup right. tempname = "%s.%d" % (socketname, os.getpid()) try: os.unlink(tempname) except OSError: pass while 1: sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: sock.bind(tempname) os.chmod(tempname, sockchmod) try: # hard link os.link(tempname, socketname) except OSError: # Lock contention, or stale socket. used = self.checkused(socketname) if used: # cooperate with 'openhttpserver' in supervisord raise socket.error(errno.EADDRINUSE) # Stale socket -- delete, sleep, and try again. msg = "Unlinking stale socket %s\n" % socketname sys.stderr.write(msg) try: os.unlink(socketname) except: pass sock.close() time.sleep(.3) continue else: try: os.chown(socketname, sockchown[0], sockchown[1]) except OSError as why: if why.args[0] == errno.EPERM: msg = ('Not permitted to chown %s to uid/gid %s; ' 'adjust "sockchown" value in config file or ' 'on command line to values that the ' 'current user (%s) can successfully chown') raise ValueError(msg % (socketname, repr(sockchown), pwd.getpwuid( os.geteuid())[0], ), ) else: raise self.prebind(sock, logger_object) break finally: try: os.unlink(tempname) except OSError: pass self.server_name = '<unix domain socket>' self.postbind()
def connect (self): s = socket.socket (socket.AF_INET, socket.SOCK_STREAM) s.connect (self.address) self.socket = s
def connect(self): self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) # we abuse the host parameter as the socketname self.sock.connect(self.socketfile)
# first grab the timestamp ts = sock.recv (1024)[:-2] sock.send (hex_digest (ts+password) + '\r\n') while 1: d = sock.recv (1024) if not d: lock.release() print('Connection closed. Hit <return> to exit') thread.exit() sys.stdout.write (d) sys.stdout.flush() def writer (lock, sock, barrel="just kidding"): while lock.locked(): sock.send ( sys.stdin.readline()[:-1] + '\r\n' ) if __name__ == '__main__': if len(sys.argv) == 1: print('Usage: %s host port') sys.exit(0) print_function('Enter Password: '******'') p = raw_input() s = socket.socket (socket.AF_INET, socket.SOCK_STREAM) s.connect((sys.argv[1], int(sys.argv[2]))) l = thread.allocate_lock() l.acquire() thread.start_new_thread (reader, (l, s, p)) writer (l, s)
def create_socket(self, family, type): self.family_and_type = family, type self.socket = socket.socket(family, type) self.socket.setblocking(0) self._fileno = self.socket.fileno() self.add_channel()
def connect(self): # pragma: no cover self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) # we abuse the host parameter as the socketname self.sock.connect(self.socketfile)
def create_and_bind(self): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(self.addr()) return sock
def __init__(self, socketname, sockchmod, sockchown, logger_object): self.ip = socketname self.port = socketname # XXX this is insecure. We really should do something like # http://developer.apple.com/samplecode/CFLocalServer/listing6.html # (see also http://developer.apple.com/technotes/tn2005/tn2083.html#SECUNIXDOMAINSOCKETS) # but it would be very inconvenient for the user to need to get all # the directory setup right. tempname = "%s.%d" % (socketname, os.getpid()) try: os.unlink(tempname) except OSError: pass while 1: sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: sock.bind(tempname) os.chmod(tempname, sockchmod) try: # hard link os.link(tempname, socketname) except OSError: # Lock contention, or stale socket. used = self.checkused(socketname) if used: # cooperate with 'openhttpserver' in supervisord raise socket.error(errno.EADDRINUSE) # Stale socket -- delete, sleep, and try again. msg = "Unlinking stale socket %s\n" % socketname sys.stderr.write(msg) try: os.unlink(socketname) except: pass sock.close() time.sleep(.3) continue else: try: os.chown(socketname, sockchown[0], sockchown[1]) except OSError as why: if why.args[0] == errno.EPERM: msg = ('Not permitted to chown %s to uid/gid %s; ' 'adjust "sockchown" value in config file or ' 'on command line to values that the ' 'current user (%s) can successfully chown') raise ValueError(msg % (socketname, repr(sockchown), pwd.getpwuid( os.geteuid())[0], ), ) else: raise self.prebind(sock, logger_object) break finally: try: os.unlink(tempname) except OSError: pass self.server_name = '<unix domain socket>' self.postbind()