本文整理匯總了Python中socket.SHUT_RD屬性的典型用法代碼示例。如果您正苦於以下問題:Python socket.SHUT_RD屬性的具體用法?Python socket.SHUT_RD怎麽用?Python socket.SHUT_RD使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類socket
的用法示例。
在下文中一共展示了socket.SHUT_RD屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: ip_latency_test
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SHUT_RD [as 別名]
def ip_latency_test(ip, port=443):
tag = 'IP_Latency_TEST'
print_with_tag(tag, ['Prepare IP latency test for ip', ip, 'Port', str(port)])
s_test = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s_test.settimeout(10)
s_start = time.time()
try:
s_test.connect((ip, port))
s_test.shutdown(socket.SHUT_RD)
except Exception as e:
print_with_tag(tag, ['Error:', e])
return None
s_stop = time.time()
s_runtime = '%.2f' % (1000 * (s_stop - s_start))
print_with_tag(tag, [ip, 'Latency:', s_runtime])
return float(s_runtime)
示例2: _rd_shutdown
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SHUT_RD [as 別名]
def _rd_shutdown(self, conn, once=False):
"""action when connection should be read-shutdown
:type conn: socket.SocketType
"""
if conn in self.conn_rd:
self.conn_rd.remove(conn)
try:
conn.shutdown(socket.SHUT_RD)
except:
pass
if not once and conn in self.map: # use the `once` param to avoid infinite loop
# if a socket is rd_shutdowned, then it's
# pair should be wr_shutdown.
self._wr_shutdown(self.map[conn], True)
if self.map.get(conn) not in self.conn_rd:
# if both two connection pair was rd-shutdowned,
# this pair sockets are regarded to be completed
# so we gonna close them
self._terminate(conn)
示例3: _close_socket_on_stop
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SHUT_RD [as 別名]
def _close_socket_on_stop(self, sock: socket.socket) -> None:
# This function runs in the background waiting for the `stop` Event to
# be set at which point it closes the socket, causing the server to
# shutdown. This allows the server threads to be cleanly closed on
# demand.
self.wait_stopped()
try:
sock.shutdown(socket.SHUT_RD)
except OSError as e:
# on mac OS this can result in the following error:
# OSError: [Errno 57] Socket is not connected
if e.errno != errno.ENOTCONN:
raise
sock.close()
示例4: tcp
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SHUT_RD [as 別名]
def tcp(host, port=80, count=1, timeout=1):
result = []
for _ in range(count):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(timeout)
start_time = time.time()
try:
s.connect((host, port))
s.shutdown(socket.SHUT_RD)
stop_time = time.time()
result.append(round(1000 * (stop_time - start_time), 2))
except socket.timeout:
result.append(-1)
except OSError as e:
print("OS Error:", e)
result.append(-1)
finally:
s.close()
return result
示例5: shutdown
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SHUT_RD [as 別名]
def shutdown(self, how=socket.SHUT_RDWR):
"""
Send a shutdown signal for one or both of reading and writing. Valid
arguments are ``socket.SHUT_RDWR``, ``socket.SHUT_RD``, and
``socket.SHUT_WR``.
Shutdown differs from closing in that it explicitly changes the state of
the socket resource to closed, whereas closing will only decrement the
number of peers on this end of the socket, since sockets can be a
resource shared by multiple peers on a single OS. When the number of
peers reaches zero, the socket is closed, but not deallocated, so you
still need to call close. (except that this is python and close is
automatically called on the deletion of the socket)
http://stackoverflow.com/questions/409783/socket-shutdown-vs-socket-close
"""
return self.sock.shutdown(how)
示例6: _shutdown_connection
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SHUT_RD [as 別名]
def _shutdown_connection(connection):
if not connection.rfile.closed:
connection.socket.shutdown(socket.SHUT_RD)
示例7: test_shutdown_connection
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SHUT_RD [as 別名]
def test_shutdown_connection(self):
class DummyObect(object):
pass
connection = DummyObect()
connection.rfile = DummyObect()
connection.rfile.closed = False
connection.socket = self.mox.CreateMockAnything()
connection.socket.shutdown(socket.SHUT_RD)
self.mox.ReplayAll()
self.thread_pool._shutdown_connection(connection)
self.mox.VerifyAll()
示例8: _get_database_tcp_latency
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SHUT_RD [as 別名]
def _get_database_tcp_latency(self, timeout: float = 5):
db_config = database.get_config()
host, port = self._get_db_host_and_port(db_config["DatabaseHost"])
# New Socket and Time out
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
# Start a timer
s_start = timer()
# Try to Connect
try:
sock.connect((host, int(port)))
sock.shutdown(socket.SHUT_RD)
sock.close()
# If something bad happens, the latency is None
except socket.timeout:
return None
except OSError:
return None
# Stop Timer
s_stop = timer()
s_runtime = "%.2f" % (1000 * (s_stop - s_start))
return s_runtime
示例9: stop
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SHUT_RD [as 別名]
def stop(self, timeout=5):
# OmniMarkupPreviewer: Force shutdown without waiting too much
while self._get_qsize() > 0:
conn = self.get()
if conn is not _SHUTDOWNREQUEST:
conn.close()
# Must shut down threads here so the code that calls
# this method can know when all threads are stopped.
for worker in self._threads:
self._queue.put(_SHUTDOWNREQUEST)
# Don't join currentThread (when stop is called inside a request).
current = threading.currentThread()
if timeout and timeout >= 0:
endtime = time.time() + timeout
while self._threads:
worker = self._threads.pop()
if worker is not current and worker.isAlive():
try:
if timeout is None or timeout < 0:
worker.join()
else:
remaining_time = endtime - time.time()
if remaining_time > 0:
worker.join(remaining_time)
if worker.isAlive():
# We exhausted the timeout.
# Forcibly shut down the socket.
c = worker.conn
if c and not c.rfile.closed:
try:
c.socket.shutdown(socket.SHUT_RD)
except TypeError:
# pyOpenSSL sockets don't take an arg
c.socket.shutdown()
worker.join()
except (AssertionError,
# Ignore repeated Ctrl-C.
# See https://bitbucket.org/cherrypy/cherrypy/issue/691.
KeyboardInterrupt):
pass
示例10: stop
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SHUT_RD [as 別名]
def stop(self, timeout=5):
# Must shut down threads here so the code that calls
# this method can know when all threads are stopped.
for worker in self._threads:
self._queue.put(_SHUTDOWNREQUEST)
# Don't join currentThread (when stop is called inside a request).
current = threading.currentThread()
if timeout and timeout >= 0:
endtime = time.time() + timeout
while self._threads:
worker = self._threads.pop()
if worker is not current and worker.isAlive():
try:
if timeout is None or timeout < 0:
worker.join()
else:
remaining_time = endtime - time.time()
if remaining_time > 0:
worker.join(remaining_time)
if worker.isAlive():
# We exhausted the timeout.
# Forcibly shut down the socket.
c = worker.conn
if c and not c.rfile.closed:
try:
c.socket.shutdown(socket.SHUT_RD)
except TypeError:
# pyOpenSSL sockets don't take an arg
c.socket.shutdown()
worker.join()
except (AssertionError,
# Ignore repeated Ctrl-C.
# See http://www.cherrypy.org/ticket/691.
KeyboardInterrupt), exc1:
pass
示例11: test_005_event
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SHUT_RD [as 別名]
def test_005_event(self):
self.writer.write(b'mgmt.event+arg dom0 name dom0\0payload')
self.writer.write_eof()
with self.assertNotRaises(asyncio.TimeoutError):
response = self.loop.run_until_complete(
asyncio.wait_for(self.reader.readuntil(b'\0\0'), 1))
self.assertEqual(response, b"1\0subject\0event\0payload\0payload\0\0")
# this will trigger connection_lost, but only when next event is sent
self.sock_client.shutdown(socket.SHUT_RD)
# check if event-producing method is interrupted
with self.assertNotRaises(asyncio.TimeoutError):
self.loop.run_until_complete(
asyncio.wait_for(self.protocol.mgmt.task, 1))
示例12: close_socket_for_read
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SHUT_RD [as 別名]
def close_socket_for_read(self):
"""Close the server-client socket for reads incoming from client.
We do not completly close the socket right now to allow late reads
in the client side. But this socket will soon be closed.
"""
if self._sock is not None:
self.outmsg("Closing socket for reads")
try:
self._sock.shutdown(socket.SHUT_RD)
except Exception:
# already closed
pass
self._sock_accept_reads = False
示例13: stop
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SHUT_RD [as 別名]
def stop(self, timeout=5):
# Must shut down threads here so the code that calls
# this method can know when all threads are stopped.
for worker in self._threads:
self._queue.put(_SHUTDOWNREQUEST)
# Don't join currentThread (when stop is called inside a request).
current = threading.currentThread()
if timeout and timeout >= 0:
endtime = time.time() + timeout
while self._threads:
worker = self._threads.pop()
if worker is not current and worker.isAlive():
try:
if timeout is None or timeout < 0:
worker.join()
else:
remaining_time = endtime - time.time()
if remaining_time > 0:
worker.join(remaining_time)
if worker.isAlive():
# We exhausted the timeout.
# Forcibly shut down the socket.
c = worker.conn
if c and not c.rfile.closed:
try:
c.socket.shutdown(socket.SHUT_RD)
except TypeError:
# pyOpenSSL sockets don't take an arg
c.socket.shutdown()
worker.join()
except (AssertionError,
# Ignore repeated Ctrl-C.
# See
# https://github.com/cherrypy/cherrypy/issues/691.
KeyboardInterrupt):
pass
示例14: server
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SHUT_RD [as 別名]
def server(self):
print "[+] Redirect with script injection initialized."
self.dnsspoof.start(None, "Inject")
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_address = (self.host, self.port)
print '[+] Injection URL - http://{}:{}'.format(self.host, self.port)
server.bind(server_address)
server.listen(1)
for i in range(0, 2):
if i >= 1:
domain = self.dnsspoof.getdomain()
domain = domain[:-1]
print "[+] Target was requesting: {}".format(domain)
self.dnsspoof.stop()
try:
connection, client_address = server.accept()
redirect = self.response + """<body> <meta http-equiv="refresh" content="0; URL='http://{}"/> </body>""".format(
domain)
connection.send("%s" % redirect)
print "[+] Script Injected on: ", client_address
connection.shutdown(socket.SHUT_WR | socket.SHUT_RD)
connection.close()
except KeyboardInterrupt:
server.close()
try:
connection, client_address = server.accept()
connection.send("%s" % self.response)
connection.shutdown(socket.SHUT_WR | socket.SHUT_RD)
connection.close()
except KeyboardInterrupt:
server.close()
示例15: cancel
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SHUT_RD [as 別名]
def cancel(self):
"""Cancel this context; gevent might complain about this with an
exception later on."""
try:
self.sock.shutdown(socket.SHUT_RD)
except socket.error:
pass