本文整理汇总了Python中socket.close方法的典型用法代码示例。如果您正苦于以下问题:Python socket.close方法的具体用法?Python socket.close怎么用?Python socket.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socket
的用法示例。
在下文中一共展示了socket.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: stop
# 需要导入模块: import socket [as 别名]
# 或者: from socket import close [as 别名]
def stop(self):
self._stop = True
if self.threads:
for t in self.threads:
t.stop()
# not so nice solution to get rid of the block of listen()
# unfortunately close() does not help on the block
try:
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverbind = self.config.get("Global", "serverbind")
if serverbind == "0.0.0.0":
# windows does not like to connect to 0.0.0.0
serverbind = "127.0.0.1"
server_socket.connect((serverbind, int(self.config.get(self.get_module_configname(), "serverport"))))
except:
pass
return
示例2: stop
# 需要导入模块: import socket [as 别名]
# 或者: from socket import close [as 别名]
def stop(self):
self._stop = True
if self.threads:
for t in self.threads:
t.stop()
# not so nice solution to get rid of the block of listen()
# unfortunately close() does not help on the block
try:
server_socket = self.sctp.sctpsocket_tcp(socket.AF_INET)
if self.config.get("Global", "serverbind") == "0.0.0.0":
server_socket.connect(("127.0.0.1", int(self.config.get(self.get_module_configname(), "serverport"))))
else:
server_socket.connect((self.config.get("Global", "serverbind"), int(self.config.get(self.get_module_configname(), "serverport"))))
except:
pass
return
示例3: sendAPRSPacket
# 需要导入模块: import socket [as 别名]
# 或者: from socket import close [as 别名]
def sendAPRSPacket(socket, packet):
"""
Sends an APRS packet (just a string) to the socket specified. If an
error occurs a False is returned while a True is returned if successful.
On an error, the socket is closed as it is no longer useful.
:param socket: APRS-IS server internet socket
:param packet: String to be sent to APRS-IS
:return: Boolean
"""
try:
socket.sendall(packet)
return True
except IOError as e:
logger.error(e)
socket.close()
return False
示例4: close
# 需要导入模块: import socket [as 别名]
# 或者: from socket import close [as 别名]
def close(self):
"""Close the socket underlying this connection."""
self.rfile.close()
if not self.linger:
# Python's socket module does NOT call close on the kernel socket
# when you call socket.close(). We do so manually here because we
# want this server to send a FIN TCP segment immediately. Note this
# must be called *before* calling socket.close(), because the latter
# drops its reference to the kernel socket.
# Python 3 *probably* fixed this with socket._real_close; hard to tell.
## self.socket._sock.close()
self.socket.close()
else:
# On the other hand, sometimes we want to hang around for a bit
# to make sure the client has a chance to read our entire
# response. Skipping the close() calls here delays the FIN
# packet until the socket object is garbage-collected later.
# Someday, perhaps, we'll do the full lingering_close that
# Apache does, but not today.
pass
示例5: respond
# 需要导入模块: import socket [as 别名]
# 或者: from socket import close [as 别名]
def respond(self):
"""Process the current request."""
response = self.req.server.wsgi_app(self.env, self.start_response)
try:
for chunk in response:
# "The start_response callable must not actually transmit
# the response headers. Instead, it must store them for the
# server or gateway to transmit only after the first
# iteration of the application return value that yields
# a NON-EMPTY string, or upon the application's first
# invocation of the write() callable." (PEP 333)
if chunk:
if isinstance(chunk, unicodestr):
chunk = chunk.encode('ISO-8859-1')
self.write(chunk)
finally:
if hasattr(response, "close"):
response.close()
示例6: close
# 需要导入模块: import socket [as 别名]
# 或者: from socket import close [as 别名]
def close(self):
"""Close the socket underlying this connection."""
self.rfile.close()
if not self.linger:
# Python's socket module does NOT call close on the kernel socket
# when you call socket.close(). We do so manually here because we
# want this server to send a FIN TCP segment immediately. Note this
# must be called *before* calling socket.close(), because the latter
# drops its reference to the kernel socket.
if hasattr(self.socket, '_sock'):
self.socket._sock.close()
self.socket.close()
else:
# On the other hand, sometimes we want to hang around for a bit
# to make sure the client has a chance to read our entire
# response. Skipping the close() calls here delays the FIN
# packet until the socket object is garbage-collected later.
# Someday, perhaps, we'll do the full lingering_close that
# Apache does, but not today.
pass
示例7: run
# 需要导入模块: import socket [as 别名]
# 或者: from socket import close [as 别名]
def run(self):
self.server.stats['Worker Threads'][self.getName()] = self.stats
try:
self.ready = True
while True:
conn = self.server.requests.get()
if conn is _SHUTDOWNREQUEST:
return
self.conn = conn
if self.server.stats['Enabled']:
self.start_time = time.time()
try:
conn.communicate()
finally:
conn.close()
if self.server.stats['Enabled']:
self.requests_seen += self.conn.requests_seen
self.bytes_read += self.conn.rfile.bytes_read
self.bytes_written += self.conn.wfile.bytes_written
self.work_time += time.time() - self.start_time
self.start_time = None
self.conn = None
except (KeyboardInterrupt, SystemExit), exc:
self.server.interrupt = exc
示例8: respond
# 需要导入模块: import socket [as 别名]
# 或者: from socket import close [as 别名]
def respond(self):
response = self.req.server.wsgi_app(self.env, self.start_response)
try:
for chunk in response:
# "The start_response callable must not actually transmit
# the response headers. Instead, it must store them for the
# server or gateway to transmit only after the first
# iteration of the application return value that yields
# a NON-EMPTY string, or upon the application's first
# invocation of the write() callable." (PEP 333)
if chunk:
if isinstance(chunk, unicode):
chunk = chunk.encode('ISO-8859-1')
self.write(chunk)
finally:
if hasattr(response, "close"):
response.close()
示例9: cleanup
# 需要导入模块: import socket [as 别名]
# 或者: from socket import close [as 别名]
def cleanup(self):
try:
self.comms_socket.close()
except:
pass
if self.serverorclient:
self.packetselector.delete_client(self.client)
示例10: cleanup
# 需要导入模块: import socket [as 别名]
# 或者: from socket import close [as 别名]
def cleanup(self, socket):
common.internal_print("Shutting down module: {0}".format(self.get_module_name()))
socket.close()
return
示例11: delete_client
# 需要导入模块: import socket [as 别名]
# 或者: from socket import close [as 别名]
def delete_client(self, client):
if client in self.clients:
if self.os_type == common.OS_WINDOWS:
import win32file
try:
win32file.CloseHandle(client.get_pipe_r())
win32file.CloseHandle(client.get_pipe_w())
except Exception as e:
common.internal_print("Remove authenticated client: CloseHandle exception: {0}".format(e), -1)
else:
try:
client.get_pipe_r_fd().close()
client.get_pipe_w_fd().close()
except Exception as e:
common.internal_print("Remove authenticated client: os.close exception: {0}".format(e), -1)
client.call_stopfp()
self.clients.remove(client)
return
# This function should run from the point when the framework was started.
# It runs as an infinite loop to read the packets off the tunnel.
# When an IPv4 packet was found that will be selected and checked whether
# it addresses a client in the client list. If a client was found, then the
# packet will be written on that pipe.
示例12: shutdown_request
# 需要导入模块: import socket [as 别名]
# 或者: from socket import close [as 别名]
def shutdown_request(self, request):
"""Called to shutdown and close an individual request."""
self.close_request(request)
示例13: server_close
# 需要导入模块: import socket [as 别名]
# 或者: from socket import close [as 别名]
def server_close(self):
"""Called to clean-up the server.
May be overridden.
"""
self.socket.close()
示例14: close_request
# 需要导入模块: import socket [as 别名]
# 或者: from socket import close [as 别名]
def close_request(self, request):
"""Called to clean up an individual request."""
request.close()
示例15: tearDown
# 需要导入模块: import socket [as 别名]
# 或者: from socket import close [as 别名]
def tearDown(self):
self.resolver.close()
super(ThreadedResolverTest, self).tearDown()