当前位置: 首页>>代码示例>>Python>>正文


Python socket.SO_LINGER属性代码示例

本文整理汇总了Python中socket.SO_LINGER属性的典型用法代码示例。如果您正苦于以下问题:Python socket.SO_LINGER属性的具体用法?Python socket.SO_LINGER怎么用?Python socket.SO_LINGER使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在socket的用法示例。


在下文中一共展示了socket.SO_LINGER属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_quick_connect

# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_LINGER [as 别名]
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() 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:21,代码来源:test_asyncore.py

示例2: run

# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_LINGER [as 别名]
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") 
开发者ID:kd8bny,项目名称:LiMEaide,代码行数:26,代码来源:tcp_client.py

示例3: test_quick_connect

# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_LINGER [as 别名]
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() 
开发者ID:dxwu,项目名称:BinderFilter,代码行数:20,代码来源:test_asyncore.py

示例4: _closeSocket

# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_LINGER [as 别名]
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 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:25,代码来源:tcp.py

示例5: test_quick_connect

# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_LINGER [as 别名]
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() 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:25,代码来源:test_asyncore.py

示例6: connect

# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_LINGER [as 别名]
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 
开发者ID:Swind,项目名称:pure-python-adb,代码行数:24,代码来源:connection.py

示例7: close

# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_LINGER [as 别名]
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() 
开发者ID:kenorb-contrib,项目名称:BitTorrent,代码行数:26,代码来源:RawServer_twisted.py

示例8: set_sock_linger

# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_LINGER [as 别名]
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
        )
    ) 
开发者ID:baidu,项目名称:CUP,代码行数:20,代码来源:__init__.py

示例9: testSO_KEEPALIVE

# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_LINGER [as 别名]
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]) 
开发者ID:Acmesec,项目名称:CTFCrackTools-V2,代码行数:22,代码来源:test_socket.py

示例10: test_quick_connect

# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_LINGER [as 别名]
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() 
开发者ID:Acmesec,项目名称:CTFCrackTools-V2,代码行数:19,代码来源:test_asyncore.py

示例11: tcp_scan

# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_LINGER [as 别名]
def tcp_scan((target, port)):

    target, port = (target, port)

    try:
        conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	conn.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1,0))
        conn.settimeout(timeout)

        ret = conn.connect_ex((target, port))

	# DATA RECIEVED - SYN ACK
        if (ret==0):
            if VERBOSE:
                sys.stdout.write("[%s] %s - %d/tcp open (SYN-ACK packet)\n" % (date_time(), target, port))
            ports_ident["open"].append(port)
	
	# RST RECIEVED - PORT CLOSED
	elif (ret == 111):
            if VERBOSE:
                sys.stdout.write("[%s] %s - %d/tcp closed (RST packet)\n" % (date_time(), target, port))
            ports_ident["closed"].append(port)
	
	# ERR CODE 11 - TIMEOUT
	elif (ret == 11):
           ports_ident["filtered"].append(port)
 	            
	else:
            if VERBOSE:
		print port
    except socket.timeout:
	ports_ident["filtered"].append(port)
        
    conn.close() 
开发者ID:m57,项目名称:piescan,代码行数:36,代码来源:piescan.py

示例12: get_free_port

# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_LINGER [as 别名]
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 
开发者ID:spyder-ide,项目名称:spyder-kernels,代码行数:10,代码来源:frontendcomm.py

示例13: checkPortAvailable

# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_LINGER [as 别名]
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() 
开发者ID:hyperledger,项目名称:indy-plenum,代码行数:28,代码来源:util.py

示例14: set_instant_rst

# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_LINGER [as 别名]
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) 
开发者ID:alexbers,项目名称:mtprotoproxy,代码行数:6,代码来源:mtprotoproxy.py

示例15: test_quick_connect

# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_LINGER [as 别名]
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) 
开发者ID:aliyun,项目名称:oss-ftp,代码行数:28,代码来源:test_functional.py


注:本文中的socket.SO_LINGER属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。