本文整理匯總了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()
示例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")
示例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()
示例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
示例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()
示例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
示例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()
示例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
)
)
示例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])
示例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()
示例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()
示例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
示例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()
示例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)
示例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)