Python socket.SO_LINGER Examples
The following are 30
code examples of socket.SO_LINGER().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
socket
, or try the search function
.
Example #1
Source File: tcp_client.py From LiMEaide with GNU General Public License v3.0 | 7 votes |
def run(self): retry = True self.logger.info("Connecting to Socket") sel = selectors.DefaultSelector() conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) conn.setsockopt( socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) conn.setblocking(False) conn.connect_ex((self.ip, self.port)) sel.register(conn, selectors.EVENT_READ, data=None) while retry: events = sel.select() for key, mask in events: retry = self.__handle_client__(key, mask, sel) sel.unregister(conn) if self.result['success']: conn.shutdown(socket.SHUT_RDWR) self.qresult.put(self.result) self.logger.info("Socket Closed")
Example #2
Source File: connection.py From pure-python-adb with MIT License | 6 votes |
def connect(self): logger.debug("Connect to adb server - {}:{}".format(self.host, self.port)) self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) l_onoff = 1 l_linger = 0 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', l_onoff, l_linger)) if self.timeout: self.socket.settimeout(self.timeout) try: self.socket.connect((self.host, self.port)) except socket.error as e: raise RuntimeError("ERROR: connecting to {}:{} {}.\nIs adb running on your computer?".format( self.host, self.port, e )) return self.socket
Example #3
Source File: test_asyncore.py From CTFCrackTools with GNU General Public License v3.0 | 6 votes |
def test_quick_connect(self): # see: http://bugs.python.org/issue10340 server = TCPServer() t = threading.Thread(target=lambda: asyncore.loop(timeout=0.1, count=500)) t.start() for x in xrange(20): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(.2) s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) try: s.connect(server.address) except socket.error: pass finally: s.close()
Example #4
Source File: test_socket.py From CTFCrackTools with GNU General Public License v3.0 | 6 votes |
def testSO_KEEPALIVE(self): self.test_tcp_client = 1 self.test_tcp_server = 1 self._testOption(socket.SOL_SOCKET, socket.SO_KEEPALIVE, [0, 1]) self._testInheritedOption(socket.SOL_SOCKET, socket.SO_KEEPALIVE, [0, 1]) # def testSO_LINGER(self): # self.test_tcp_client = 1 # self.test_tcp_server = 1 # off = struct.pack('ii', 0, 0) # on_2_seconds = struct.pack('ii', 1, 2) # self._testOption(socket.SOL_SOCKET, socket.SO_LINGER, [off, on_2_seconds]) # self._testInheritedOption(socket.SOL_SOCKET, socket.SO_LINGER, [off, on_2_seconds]) # # WILL NOT FIX # def testSO_OOBINLINE(self): # self.test_tcp_client = 1 # self.test_tcp_server = 1 # self._testOption(socket.SOL_SOCKET, socket.SO_OOBINLINE, [0, 1]) # self._testInheritedOption(socket.SOL_SOCKET, socket.SO_OOBINLINE, [0, 1])
Example #5
Source File: test_asyncore.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 6 votes |
def test_quick_connect(self): # see: http://bugs.python.org/issue10340 server = TCPServer() t = threading.Thread(target=lambda: asyncore.loop(timeout=0.1, count=500)) t.start() for x in xrange(20): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(.2) s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) try: s.connect(server.address) except socket.error: pass finally: s.close()
Example #6
Source File: test_socket.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 6 votes |
def testSO_KEEPALIVE(self): self.test_tcp_client = 1 self.test_tcp_server = 1 self._testOption(socket.SOL_SOCKET, socket.SO_KEEPALIVE, [0, 1]) self._testInheritedOption(socket.SOL_SOCKET, socket.SO_KEEPALIVE, [0, 1]) # def testSO_LINGER(self): # self.test_tcp_client = 1 # self.test_tcp_server = 1 # off = struct.pack('ii', 0, 0) # on_2_seconds = struct.pack('ii', 1, 2) # self._testOption(socket.SOL_SOCKET, socket.SO_LINGER, [off, on_2_seconds]) # self._testInheritedOption(socket.SOL_SOCKET, socket.SO_LINGER, [off, on_2_seconds]) # # WILL NOT FIX # def testSO_OOBINLINE(self): # self.test_tcp_client = 1 # self.test_tcp_server = 1 # self._testOption(socket.SOL_SOCKET, socket.SO_OOBINLINE, [0, 1]) # self._testInheritedOption(socket.SOL_SOCKET, socket.SO_OOBINLINE, [0, 1])
Example #7
Source File: __init__.py From CUP with Apache License 2.0 | 6 votes |
def set_sock_linger(sock, l_onoff=1, l_linger=0): """ set socket linger param (socket.SO_LINGER) I.g. :: sock.setsockopt( socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 0, 0) ) """ # l_onoff = 1 # l_linger = 0 sock.setsockopt( socket.SOL_SOCKET, socket.SO_LINGER, struct.pack( 'ii', l_onoff, l_linger ) )
Example #8
Source File: RawServer_twisted.py From BitTorrent with GNU General Public License v3.0 | 6 votes |
def close(self): self.stopProducing() if self.rawserver.config.get('close_with_rst', True): try: s = self.get_socket() s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, NOLINGER) except: pass if self.transport: try: self.transport.unregisterProducer() except KeyError: # bug in iocpreactor: http://twistedmatrix.com/trac/ticket/1657 pass if (hasattr(self.transport, 'protocol') and isinstance(self.transport.protocol, CallbackDatagramProtocol)): # udp connections should only call stopListening self.transport.stopListening() else: self.transport.loseConnection() elif self.connector: self.connector.disconnect()
Example #9
Source File: test_asyncore.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_quick_connect(self): # see: http://bugs.python.org/issue10340 if self.family in (socket.AF_INET, getattr(socket, "AF_INET6", object())): server = BaseServer(self.family, self.addr) t = threading.Thread(target=lambda: asyncore.loop(timeout=0.1, count=500)) t.start() def cleanup(): t.join(timeout=TIMEOUT) if t.is_alive(): self.fail("join() timed out") self.addCleanup(cleanup) s = socket.socket(self.family, socket.SOCK_STREAM) s.settimeout(.2) s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) try: s.connect(server.address) except OSError: pass finally: s.close()
Example #10
Source File: test_asyncore.py From gcblue with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_quick_connect(self): # see: http://bugs.python.org/issue10340 server = TCPServer() t = threading.Thread(target=lambda: asyncore.loop(timeout=0.1, count=500)) t.start() self.addCleanup(t.join) for x in xrange(20): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(.2) s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) try: s.connect(server.address) except socket.error: pass finally: s.close()
Example #11
Source File: test_asyncore.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_quick_connect(self): # see: http://bugs.python.org/issue10340 if self.family in (socket.AF_INET, getattr(socket, "AF_INET6", object())): server = BaseServer(self.family, self.addr) t = threading.Thread(target=lambda: asyncore.loop(timeout=0.1, count=500)) t.start() def cleanup(): t.join(timeout=TIMEOUT) if t.is_alive(): self.fail("join() timed out") self.addCleanup(cleanup) s = socket.socket(self.family, socket.SOCK_STREAM) s.settimeout(.2) s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) try: s.connect(server.address) except OSError: pass finally: s.close()
Example #12
Source File: tcp.py From learn_python3_spider with MIT License | 6 votes |
def _closeSocket(self, orderly): # The call to shutdown() before close() isn't really necessary, because # we set FD_CLOEXEC now, which will ensure this is the only process # holding the FD, thus ensuring close() really will shutdown the TCP # socket. However, do it anyways, just to be safe. skt = self.socket try: if orderly: if self._shouldShutdown: skt.shutdown(2) else: # Set SO_LINGER to 1,0 which, by convention, causes a # connection reset to be sent when close is called, # instead of the standard FIN shutdown sequence. self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0)) except socket.error: pass try: skt.close() except socket.error: pass
Example #13
Source File: test_asyncore.py From ironpython2 with Apache License 2.0 | 6 votes |
def test_quick_connect(self): # see: http://bugs.python.org/issue10340 server = TCPServer() t = threading.Thread(target=lambda: asyncore.loop(timeout=0.1, count=500)) t.start() try: for x in xrange(20): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(.2) s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) try: s.connect(server.address) except socket.error: pass finally: s.close() finally: t.join()
Example #14
Source File: tcp.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def _closeSocket(self, orderly): # The call to shutdown() before close() isn't really necessary, because # we set FD_CLOEXEC now, which will ensure this is the only process # holding the FD, thus ensuring close() really will shutdown the TCP # socket. However, do it anyways, just to be safe. skt = self.socket try: if orderly: if self._shouldShutdown: skt.shutdown(2) else: # Set SO_LINGER to 1,0 which, by convention, causes a # connection reset to be sent when close is called, # instead of the standard FIN shutdown sequence. self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0)) except socket.error: pass try: skt.close() except socket.error: pass
Example #15
Source File: frontendcomm.py From spyder-kernels with MIT License | 6 votes |
def get_free_port(): """Find a free port on the local machine.""" sock = socket.socket() sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, b'\0' * 8) sock.bind((localhost(), 0)) port = sock.getsockname()[1] sock.close() return port
Example #16
Source File: test_asyncore.py From oss-ftp with MIT License | 6 votes |
def test_quick_connect(self): # see: http://bugs.python.org/issue10340 server = TCPServer() t = threading.Thread(target=lambda: asyncore.loop(timeout=0.1, count=500)) t.start() self.addCleanup(t.join) for x in xrange(20): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(.2) s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) try: s.connect(server.address) except socket.error: pass finally: s.close()
Example #17
Source File: test_asyncore.py From BinderFilter with MIT License | 6 votes |
def test_quick_connect(self): # see: http://bugs.python.org/issue10340 server = TCPServer() t = threading.Thread(target=lambda: asyncore.loop(timeout=0.1, count=500)) t.start() self.addCleanup(t.join) for x in xrange(20): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(.2) s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) try: s.connect(server.address) except socket.error: pass finally: s.close()
Example #18
Source File: test_asyncore.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_quick_connect(self): # see: http://bugs.python.org/issue10340 if self.family in (socket.AF_INET, getattr(socket, "AF_INET6", object())): server = BaseServer(self.family, self.addr) t = threading.Thread(target=lambda: asyncore.loop(timeout=0.1, count=500)) t.start() def cleanup(): t.join(timeout=TIMEOUT) if t.is_alive(): self.fail("join() timed out") self.addCleanup(cleanup) s = socket.socket(self.family, socket.SOCK_STREAM) s.settimeout(.2) s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) try: s.connect(server.address) except OSError: pass finally: s.close()
Example #19
Source File: connectionPool.py From BBScan with Apache License 2.0 | 5 votes |
def close(self): """ Close all pooled connections and disable the pool. """ # Disable access to the pool old_pool, self.pool = self.pool, None try: while True: conn = old_pool.get(block=False) if conn: conn.sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) conn.close() except Empty: pass
Example #20
Source File: pedrpc.py From boofuzz with GNU General Public License v2.0 | 5 votes |
def __connect(self): """ Connect to the PED-RPC server. """ # if we have a pre-existing server socket, ensure it's closed. self.__disconnect() # connect to the server, timeout on failure. self.__server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.__server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.__server_sock.settimeout(3.0) try: self.__server_sock.connect((self.__host, self.__port)) except socket.error as e: if self.__retry != 5: self.__retry += 1 time.sleep(5) self.__connect() else: raise exception.BoofuzzRpcError( 'PED-RPC> unable to connect to server {0}:{1}. Error message: "{2}"\n'.format( self.__host, self.__port, e ) ) # disable timeouts and lingering. self.__server_sock.settimeout(None) self.__server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, self.NOLINGER)
Example #21
Source File: socket.py From thriftpy2 with MIT License | 5 votes |
def _init_sock(self): if self.unix_socket: _sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) else: _sock = socket.socket(self.socket_family, socket.SOCK_STREAM) _sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) # socket options linger = struct.pack('ii', 0, 0) _sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, linger) _sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) self.raw_sock = _sock
Example #22
Source File: socket.py From thriftpy2 with MIT License | 5 votes |
def _init_sock(self): if self.unix_socket: _sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) else: _sock = socket.socket(self.socket_family, socket.SOCK_STREAM) _sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) # socket options linger = struct.pack('ii', 0, 0) _sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, linger) _sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) self.sock = _sock
Example #23
Source File: sslsocket.py From thriftpy2 with MIT License | 5 votes |
def _init_sock(self): _sock = socket.socket(self.socket_family, socket.SOCK_STREAM) _sock = self.ssl_context.wrap_socket(_sock, server_hostname=self.host) # socket options linger = struct.pack('ii', 0, 0) _sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, linger) _sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) _sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) self.sock = _sock
Example #24
Source File: connect_manager.py From XX-Net-mini with GNU General Public License v3.0 | 5 votes |
def create_connect(self, queue, host, ip, port, timeout=5): if int(g.config.PROXY_ENABLE): sock = socks.socksocket(socket.AF_INET if ':' not in ip else socket.AF_INET6) else: sock = socket.socket(socket.AF_INET if ':' not in ip else socket.AF_INET6) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # set struct linger{l_onoff=1,l_linger=0} to avoid 10048 socket error sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) # resize socket recv buffer 8K->32K to improve browser releated application performance sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 32 * 1024) sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, True) sock.settimeout(timeout) start_time = time.time() try: sock.connect((ip, port)) time_cost = (time.time() - start_time) * 1000 # xlog.debug("connect %s %s:%d time:%d", host, ip, port, time_cost) g.ip_cache.update_connect_time(ip, port, time_cost) s = SocketWrap(sock, ip, port, host) host_port = "%s:%d" % (host, port) self.add_sock(host_port, s) queue.put(True) except Exception as e: # xlog.debug("connect %s %s:%d fail:%r", host, ip, port, e) g.ip_cache.report_connect_fail(ip, port) queue.put(False)
Example #25
Source File: test_functional.py From pyftpdlib with MIT License | 5 votes |
def test_quick_connect(self): # Clients that connected and disconnected quickly could cause # the server to crash, due to a failure to catch errors in the # initial part of the connection process. # Tracked in issues #91, #104 and #105. # See also https://bugs.launchpad.net/zodb/+bug/135108 import struct def connect(addr): with contextlib.closing(socket.socket()) as s: # Set SO_LINGER to 1,0 causes a connection reset (RST) to # be sent when close() is called, instead of the standard # FIN shutdown sequence. s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) s.settimeout(TIMEOUT) try: s.connect(addr) except socket.error: pass for x in range(10): connect((self.server.host, self.server.port)) for x in range(10): addr = self.client.makepasv() connect(addr)
Example #26
Source File: util.py From indy-plenum with Apache License 2.0 | 5 votes |
def checkPortAvailable(ha): """Checks whether the given port is available""" # Not sure why OS would allow binding to one type and not other. # Checking for port available for TCP and UDP. sockTypes = (socket.SOCK_DGRAM, socket.SOCK_STREAM) for typ in sockTypes: sock = socket.socket(socket.AF_INET, typ) try: sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(ha) if typ == socket.SOCK_STREAM: l_onoff = 1 l_linger = 0 sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', l_onoff, l_linger)) except OSError as exc: if exc.errno in [ errno.EADDRINUSE, errno.EADDRNOTAVAIL, WS_SOCKET_BIND_ERROR_ALREADY_IN_USE, WS_SOCKET_BIND_ERROR_NOT_AVAILABLE ]: raise PortNotAvailable(ha) else: raise exc finally: sock.close()
Example #27
Source File: mtprotoproxy.py From mtprotoproxy with MIT License | 5 votes |
def set_instant_rst(sock): INSTANT_RST = b"\x01\x00\x00\x00\x00\x00\x00\x00" if hasattr(socket, "SO_LINGER"): try_setsockopt(sock, socket.SOL_SOCKET, socket.SO_LINGER, INSTANT_RST)
Example #28
Source File: connectionPool.py From BBScan with Apache License 2.0 | 5 votes |
def close(self): """ Close all pooled connections and disable the pool. """ # Disable access to the pool old_pool, self.pool = self.pool, None try: while True: conn = old_pool.get(block=False) if conn: conn.sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) conn.close() except Empty: pass
Example #29
Source File: test_functional.py From oss-ftp with MIT License | 5 votes |
def test_quick_connect(self): # Clients that connected and disconnected quickly could cause # the server to crash, due to a failure to catch errors in the # initial part of the connection process. # Tracked in issues #91, #104 and #105. # See also https://bugs.launchpad.net/zodb/+bug/135108 import struct def connect(addr): with contextlib.closing(socket.socket()) as s: # Set SO_LINGER to 1,0 causes a connection reset (RST) to # be sent when close() is called, instead of the standard # FIN shutdown sequence. s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0)) s.settimeout(TIMEOUT) try: s.connect(addr) except socket.error: pass for x in range(10): connect((self.server.host, self.server.port)) for x in range(10): addr = self.client.makepasv() connect(addr)
Example #30
Source File: test_socket.py From medicare-demo with Apache License 2.0 | 5 votes |
def testSO_LINGER(self): self.test_tcp_client = 1 off = struct.pack('ii', 0, 0) on_2_seconds = struct.pack('ii', 1, 2) self._testOption(socket.SOL_SOCKET, socket.SO_LINGER, [off, on_2_seconds])