我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用socket.TCP_NODELAY。
def handle(self): client_ip = self.client_address[0] addr = '' server = '' try: sock = self.connection sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) odestdata = sock.getsockopt(socket.SOL_IP, 80, 16) port, addr_ip = struct.unpack("!xxH4sxxxxxxxx", odestdata) addr = socket.inet_ntoa(addr_ip) server = reverse(addr) print_log('%s connecting %s:%d %d %s' % (client_ip, addr, port, server[0], str(server[1]))) Proxy[server[0]].proxy(sock, server[1], (addr, port)) except socket.error, e: logging.warn(addr + ':' + str(server) + ':' + str(e)) sock.close()
def _new_conn(self): """ Establish a socket connection and set nodelay settings on it :return: a new socket connection """ try: conn = socket.create_connection( (self.host, self.port), self.timeout, self.source_address, ) except AttributeError: # Python 2.6 conn = socket.create_connection( (self.host, self.port), self.timeout, ) conn.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, self.tcp_nodelay) return conn
def _create_remote_socket(self, ip, port): addrs = socket.getaddrinfo(ip, port, 0, socket.SOCK_STREAM, socket.SOL_TCP) if len(addrs) == 0: raise Exception("getaddrinfo failed for %s:%d" % (ip, port)) af, socktype, proto, canonname, sa = addrs[0] if self._forbidden_iplist: if common.to_str(sa[0]) in self._forbidden_iplist: raise Exception('IP %s is in forbidden list, reject' % common.to_str(sa[0])) remote_sock = socket.socket(af, socktype, proto) self._remote_sock = remote_sock self._fd_to_handlers[remote_sock.fileno()] = self remote_sock.setblocking(False) remote_sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) return remote_sock
def open(self): """Mostly copied from TSocket.open, with TCP_NODELAY on.""" try: res0 = self._resolveAddr() for res in res0: self.handle = socket.socket(res[0], res[1]) self.handle.settimeout(self._timeout) # turn on TCP_NODELAY self.handle.setsockopt( socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) try: self.handle.connect(res[4]) except socket.error: if res is not res0[-1]: continue else: raise break except socket.error: if self._unix_socket: message = 'Could not connect to socket %s' % self._unix_socket else: message = 'Could not connect to %s:%d' % (self.host, self.port) raise TTransportException( type=TTransportException.NOT_OPEN, message=message)
def connect(self, host, port): self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.__socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2 ** 13) self.__socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 2 ** 13) self.__socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) self.__socket.setblocking(0) self.__readBuffer = '' self.__writeBuffer = '' self.__lastReadTime = time.time() try: self.__socket.connect((host, port)) except socket.error as e: if e.errno != socket.errno.EINPROGRESS: return False self.__fileno = self.__socket.fileno() self.__disconnected = False return True
def __onNewConnection(self, localDescr, event): if event & POLL_EVENT_TYPE.READ: try: sock, addr = self.__socket.accept() sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, self.__conf.sendBufferSize) sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, self.__conf.recvBufferSize) sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) sock.setblocking(0) conn = Connection(socket=sock, timeout=self.__conf.connectionTimeout) descr = conn.fileno() self.__unknownConnections[descr] = conn self.__poller.subscribe(descr, self.__processUnknownConnections, POLL_EVENT_TYPE.READ | POLL_EVENT_TYPE.ERROR) except socket.error as e: if e.errno != socket.errno.EAGAIN: self.__isInitialized = False LOG_WARNING('Error in main socket:' + str(e)) if event & POLL_EVENT_TYPE.ERROR: self.__isInitialized = False LOG_WARNING('Error in main socket')
def _connect(self): try: if self.unix_socket: raise NotImplementedError() else: sock = greensocket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((self.host, self.port)) self.host_info = "socket %s:%d" % (self.host, self.port) if self.no_delay: sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) self.socket = sock self.rfile = self.socket.makefile("rb") self.wfile = self.socket.makefile("wb") self._get_server_information() self._request_authentication() self._send_autocommit_mode() except socket.error as e: raise Exception( 2003, "Can't connect to MySQL server on %r (%s)" % ( self.host, e.args[0]))
def modify_buff_size(): sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) # Get the size of the socket's send buffer bufsize = sock.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF) print ("Buffer size [Before]:%d" %bufsize) sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) sock.setsockopt( socket.SOL_SOCKET, socket.SO_SNDBUF, SEND_BUF_SIZE) sock.setsockopt( socket.SOL_SOCKET, socket.SO_RCVBUF, RECV_BUF_SIZE) bufsize = sock.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF) print ("Buffer size [After]:%d" %bufsize)
def __init__(self): self.wakeup_sock, self.write_sock = socket.socketpair() self.wakeup_sock.setblocking(False) self.write_sock.setblocking(False) # This somewhat reduces the amount of memory wasted queueing up data # for wakeups. With these settings, maximum number of 1-byte sends # before getting BlockingIOError: # Linux 4.8: 6 # MacOS (darwin 15.5): 1 # Windows 10: 525347 # Windows you're weird. (And on Windows setting SNDBUF to 0 makes send # blocking, even on non-blocking sockets, so don't do that.) self.wakeup_sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1) self.write_sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1) # On Windows this is a TCP socket so this might matter. On other # platforms this fails b/c AF_UNIX sockets aren't actually TCP. try: self.write_sock.setsockopt( socket.IPPROTO_TCP, socket.TCP_NODELAY, 1 ) except OSError: pass
def connect(self, host, port): family, socktype, proto, _, sockaddr = socket.getaddrinfo(host, port, self.family, self.socktype)[0] s = socket.socket(family, socktype, proto) s.settimeout(self.timeout) s.connect(sockaddr) if self.nodelay: s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) if self.keepalive: s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # Linux specific: after 1 idle minutes, start sending keepalives every 5 minutes. # Drop connection after 10 failed keepalives if hasattr(socket, "TCP_KEEPIDLE") and hasattr(socket, "TCP_KEEPINTVL") and hasattr(socket, "TCP_KEEPCNT"): s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 1 * 60) s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 5 * 60) s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 10) self.sock=s return s
def connect(self, host, port): s = socks.socksocket() s.setproxy(proxy_type=socks.PROXY_TYPES[self.proxy_type], addr=self.proxy_addr, port=self.proxy_port, rdns=True, username=self.proxy_username, password=self.proxy_password) s.settimeout(self.timeout) s.connect((host,port)) if self.nodelay: s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) if self.keepalive: s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # Linux specific: after 10 idle minutes, start sending keepalives every 5 minutes. # Drop connection after 10 failed keepalives if hasattr(socket, "TCP_KEEPIDLE") and hasattr(socket, "TCP_KEEPINTVL") and hasattr(socket, "TCP_KEEPCNT"): s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 10 * 60) s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 5 * 60) s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 10) self.sock=s return s
def bind(self, family, type, proto=0): """Create (or recreate) the actual socket object.""" self.socket = socket.socket(family, type, proto) prevent_socket_inheritance(self.socket) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) if self.nodelay and not isinstance(self.bind_addr, str): self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) if self.ssl_adapter is not None: self.socket = self.ssl_adapter.bind(self.socket) # If listening on the IPV6 any address ('::' = IN6ADDR_ANY), # activate dual-stack. See https://bitbucket.org/cherrypy/cherrypy/issue/871. if (hasattr(socket, 'AF_INET6') and family == socket.AF_INET6 and self.bind_addr[0] in ('::', '::0', '::0.0.0.0')): try: self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0) except (AttributeError, socket.error): # Apparently, the socket option is not available in # this machine's TCP stack pass self.socket.bind(self.bind_addr)
def active_connect( self ): """ Actively connect to a switch IP addr """ try: self.logger.info( "Trying active connection to %s" % self.switch ) soc = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) soc.connect( (self.switch, self.port) ) self.logger.info( "Connected to " + self.switch + " on " + str( self.port ) ) soc.setsockopt( socket.IPPROTO_TCP, socket.TCP_NODELAY, True ) self.switch_addr = (self.switch, self.port) return soc except (StandardError, socket.error), e: self.logger.error( "Could not connect to %s at %d:: %s" % (self.switch, self.port, str( e )) ) return None
def connect(ip, port=6653, daemon=True, ofp=loxi.of14): """ Actively connect to a switch """ soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) soc.connect((ip, port)) soc.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True) cxn = Connection(soc) cxn.daemon = daemon cxn.logger.debug("Connected to %s:%d", ip, port) cxn.start() cxn.send(ofp.message.hello()) if not cxn.recv(lambda msg: msg.type == ofp.OFPT_HELLO): raise Exception("Did not receive HELLO") return cxn
def bind(self, family, type, proto=0): """Create (or recreate) the actual socket object.""" self.socket = socket.socket(family, type, proto) prevent_socket_inheritance(self.socket) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) if self.nodelay and not isinstance(self.bind_addr, str): self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) if self.ssl_adapter is not None: self.socket = self.ssl_adapter.bind(self.socket) # If listening on the IPV6 any address ('::' = IN6ADDR_ANY), # activate dual-stack. See http://www.cherrypy.org/ticket/871. if (hasattr(socket, 'AF_INET6') and family == socket.AF_INET6 and self.bind_addr[0] in ('::', '::0', '::0.0.0.0')): try: self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0) except (AttributeError, socket.error): # Apparently, the socket option is not available in # this machine's TCP stack pass self.socket.bind(self.bind_addr)
def connect_socket(addr, port, cacert, srvcrt, srvkey): # connect to the master for orders s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) s.connect((addr, port)) # if we have a cacert, this means we should use SSL for this connection if cacert is not None: s = sslize(s, cacert, srvcrt, srvkey, True) if not isinstance(s, SSL.Connection): return "ERROR could not initialize SSL connection: %s\n" % str(s) # wrap in non-blocking socket reader/writer class s.setblocking(False) s = libmu.socket_nb.SocketNB(s) s.do_handshake() return s
def __init__(self, socket, address): super(Datapath, self).__init__() self.socket = socket self.socket.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1) self.socket.settimeout(CONF.socket_timeout) self.address = address self.is_active = True # The limit is arbitrary. We need to limit queue size to # prevent it from eating memory up. self.send_q = hub.Queue(16) self._send_q_sem = hub.BoundedSemaphore(self.send_q.maxsize) self.echo_request_interval = CONF.echo_request_interval self.max_unreplied_echo_requests = CONF.maximum_unreplied_echo_requests self.unreplied_echo_requests = [] self.xid = random.randint(0, self.ofproto.MAX_XID) self.id = None # datapath_id is unknown yet self._ports = None self.flow_format = ofproto_v1_0.NXFF_OPENFLOW10 self.ofp_brick = ryu.base.app_manager.lookup_service_brick('ofp_event') self.set_state(HANDSHAKE_DISPATCHER)
def _connect(self): """Connect to a local server, return True on success.""" _socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # No Nagle since we are going for as close to realtime as possible _socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) # Set the timeout for initial connect, as default is too high _socket.settimeout(5.0) log.debug('connecting to %s:%d', *self.host_port) try: _socket.connect(self.host_port) except socket.timeout: log.error('timed out connecting to server') return False except IOError as e: log.error('IO Error when connecting, %s', e) return False except: log.exception('error connecting') return False else: log.debug("connected to %s:%d", *self.host_port) self.socket = _socket self._flush_buffer() return True
def handle_write(self): if self.needs_config: self.needs_config = False self.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) self.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 128 * 1024) self.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 128 * 1024) if len(self.buffer) > 0: sent = self.send(self.buffer) logging.debug('[{0:d}] TCP => {1:d} byte(s)'.format(self.client_id, sent)) self.buffer = self.buffer[sent:] if self.needs_close and len(self.buffer) == 0: self.needs_close = False self.handle_close()
def __init__(self, connected_socket, client_id): global options asyncore.dispatcher.__init__(self, connected_socket) self.client_id = client_id self.state = self.STATE_WAITING_FOR_HANDSHAKE self.ip = None self.addresses = None self.hostname = None self.port = None self.requested_address = None self.buffer = '' self.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) self.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 128 * 1024) self.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 128 * 1024) self.needs_close = False
def _connect(self): "Create a TCP socket connection" # we want to mimic what socket.create_connection does to support # ipv4/ipv6, but we want to set options prior to calling # socket.connect() err = None for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM): family, socktype, proto, canonname, socket_address = res sock = None try: sock = socket.socket(family, socktype, proto) # TCP_NODELAY sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) # TCP_KEEPALIVE if self.socket_keepalive: sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) for k, v in iteritems(self.socket_keepalive_options): sock.setsockopt(socket.SOL_TCP, k, v) # set the socket_connect_timeout before we connect sock.settimeout(self.socket_connect_timeout) # connect sock.connect(socket_address) # set the socket_timeout now that we're connected sock.settimeout(self.socket_timeout) return sock except socket.error as _: err = _ if sock is not None: sock.close() if err is not None: raise err raise socket.error("socket.getaddrinfo returned an empty list")
def setup(self): self.connection = self.request if self.timeout is not None: self.connection.settimeout(self.timeout) if self.disable_nagle_algorithm: self.connection.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True) self.rfile = self.connection.makefile('rb', self.rbufsize) self.wfile = self.connection.makefile('wb', self.wbufsize)
def proxy(sock, server, addr): if addr[1] == 80: redirect_https(sock) return remote = socket.create_connection((server[0], addr[1])) remote.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) try: fdset = [sock, remote] while True: r, w, e = select.select(fdset, [], []) if sock in r: data = sock.recv(4096) if len(data) <= 0: break result = send_all(remote, data) if result < len(data): raise Exception('failed to send all data') if remote in r: data = remote.recv(4096) if len(data) <= 0: break result = send_all(sock, data) if result < len(data): raise Exception('failed to send all data') finally: sock.close() remote.close()
def proxy(sock, server, addr): remote = socket.create_connection(addr) remote.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) try: fdset = [sock, remote] while True: r, w, e = select.select(fdset, [], []) if sock in r: data = sock.recv(4096) if len(data) <= 0: break result = send_all(remote, data) if result < len(data): raise Exception('failed to send all data') if remote in r: data = remote.recv(4096) if len(data) <= 0: break result = send_all(sock, data) if result < len(data): raise Exception('failed to send all data') finally: sock.close() remote.close()
def proxy(sock, server, addr): if isinstance(server, list): server = (server[random.randint(0, len(server)-1)], addr[1]) elif isinstance(server, str): server = (server, addr[1]) remote = socket.create_connection(server) remote.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) try: fdset = [sock, remote] while True: r, w, e = select.select(fdset, [], []) if sock in r: data = sock.recv(4096) if len(data) <= 0: break result = send_all(remote, data) if result < len(data): raise Exception('failed to send all data') if remote in r: data = remote.recv(4096) if len(data) <= 0: break result = send_all(sock, data) if result < len(data): raise Exception('failed to send all data') finally: sock.close() remote.close()
def make_conn(bld, srv): #port = PORT + idx port = srv.port conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) conn.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) conn.connect(('127.0.0.1', port)) return conn
def create_server(conn, cls): # child processes do not need the key, so we remove it from the OS environment global SHARED_KEY SHARED_KEY = os.environ['SHARED_KEY'] os.environ['SHARED_KEY'] = '' ppid = int(os.environ['PREFORKPID']) def reap(): if os.sep != '/': os.waitpid(ppid, 0) else: while 1: try: os.kill(ppid, 0) except OSError: break else: time.sleep(1) os.kill(os.getpid(), signal.SIGKILL) t = threading.Thread(target=reap) t.setDaemon(True) t.start() server = SocketServer.TCPServer(conn, req) print(server.server_address[1]) sys.stdout.flush() #server.timeout = 6000 # seconds server.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) try: server.serve_forever(poll_interval=0.001) except KeyboardInterrupt: pass
def make_conn(bld, srv): port = srv.port conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) conn.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) conn.connect(('127.0.0.1', port)) return conn
def set_nodelay(self, value): if (self.socket is not None and self.socket.family in (socket.AF_INET, socket.AF_INET6)): try: self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1 if value else 0) except socket.error as e: # Sometimes setsockopt will fail if the socket is closed # at the wrong time. This can happen with HTTPServer # resetting the value to false between requests. if e.errno != errno.EINVAL and not self._is_connreset(e): raise
def getTcpNoDelay(self): return operator.truth(self.socket.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY))