本文整理汇总了Python中SocketServer.ThreadingTCPServer.shutdown方法的典型用法代码示例。如果您正苦于以下问题:Python ThreadingTCPServer.shutdown方法的具体用法?Python ThreadingTCPServer.shutdown怎么用?Python ThreadingTCPServer.shutdown使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SocketServer.ThreadingTCPServer
的用法示例。
在下文中一共展示了ThreadingTCPServer.shutdown方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: shutdown
# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import shutdown [as 别名]
def shutdown(self):
"""Stop the server."""
# this is the call for shutting down the server
ThreadingTCPServer.shutdown(self)
# wait until all the requests have finished
for h in self.handlers.values():
h.join()
示例2: shutdown
# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import shutdown [as 别名]
def shutdown(self, args):
""" Overloaded method that kills the correlator before shutdown."""
self.logger.debug('shutdown()')
self.logger.info('shutting down the server...')
if self._started:
self.stop_correlator('')
ThreadingTCPServer.shutdown(self)
return SBYTE.pack(0)
示例3: stop
# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import shutdown [as 别名]
def stop(port=21567):
"""
this function to close the server
"""
host = ''
address = (host, port)
tcpServ = ThreadingTCPServer(address, MyRequestHandler)
print 'closing connection...'
tcpServ.shutdown()
示例4: Server
# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import shutdown [as 别名]
class Server(Thread):
def __init__(self, host="127.0.0.1", port=3794, handler=Handler):
Thread.__init__(self)
self.server = ThreadingTCPServer((host, port), handler)
def run(self):
self.server.serve_forever()
def stopRunning(self):
self.server.shutdown()
self.server.server_close()
示例5: LogServer
# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import shutdown [as 别名]
class LogServer():
'''由于serve_forever()是阻塞的,所以需要单开一个进程或线程来开启日志服务'''
def __init__(self, addr, requestHandler):
self.bindAddress = addr
self.requestHandler = requestHandler
logging.config.fileConfig(LOGCONFIG)
def start(self):
self.svr = ThreadingTCPServer(self.bindAddress, self.requestHandler)
self.svr.serve_forever()
def stop(self):
self.svr.shutdown()
示例6: ALPSTCPServer
# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import shutdown [as 别名]
class ALPSTCPServer():
bufsize = 1500
@property
def connections_list(self):
return self.connections.keys()
def __init__(self, ip, port, receiver_func=None, connection_event_func=None, background=False, thread_name="TCPServer Thread"):
self.ip_string = ip
self.port = port
self.receiver_func_callback = receiver_func
self.connection_event_callback = connection_event_func
self.background = background
self.connections = {}
self.tcpserver = ThreadingTCPServer((self.ip_string, self.port), self.__request_handle_process)
self.tcpserver.allow_reuse_address = True
tcpsock_thread = ALPSThread(threadfunc=self.tcpserver.serve_forever, threadname=thread_name)
tcpsock_thread.setDaemon(not self.background)
tcpsock_thread.start()
def __request_handle_process(self, sock_obj, client_address, server):
self.connections[client_address] = sock_obj
if callable(self.connection_event_callback):
self.connection_event_callback(self, client_address, True)
while True:
try:
data = sock_obj.recv(self.bufsize)
except:
data = ''
if data:
if callable(self.receiver_func_callback):
self.receiver_func_callback(self, client_address, data)
# print client_address
else:
self.connections.pop(client_address)
if callable(self.connection_event_callback):
self.connection_event_callback(self, client_address, False)
break
def server_close(self):
for sock_obj in self.connections.values():
self.tcpserver.shutdown_request(sock_obj)
self.tcpserver.shutdown()
self.tcpserver.server_close()
def send(self, client_address, message, error_except=True):
if client_address not in self.connections.keys():
ALPSDebug.alps_error('socket not exist error')
return False
connection = self.connections[client_address]
if not error_except:
return connection.sendall(message)
try:
connection.sendall(message)
except Exception as e:
ALPSDebug.alps_error(e, '\n', inspect.getframeinfo((inspect.currentframe().f_back)))
return False
return True
def send_to_all_connections(self, message, error_except=True):
if len(self.connections) == 0:
return True
for sock_obj in self.connections.values():
if error_except:
try:
sock_obj.sendall(message)
except Exception as e:
ALPSDebug.alps_error(e, '\n', inspect.getframeinfo((inspect.currentframe().f_back)))
return False
else:
sock_obj.sendall(message)
return True
示例7: PySAPNIStreamSocketTest
# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import shutdown [as 别名]
class PySAPNIStreamSocketTest(unittest.TestCase):
test_port = 8005
test_address = "127.0.0.1"
test_string = "TEST" * 10
def start_server(self, handler_cls):
self.server = ThreadingTCPServer((self.test_address, self.test_port),
handler_cls,
bind_and_activate=False)
self.server.allow_reuse_address = True
self.server.server_bind()
self.server.server_activate()
self.server_thread = Thread(target=self.server.serve_forever)
self.server_thread.start()
def stop_server(self):
self.server.shutdown()
self.server.server_close()
self.server_thread.join()
def test_sapnistreamsocket(self):
"""Test SAPNIStreamSocket"""
self.start_server(SAPNITestHandler)
sock = socket.socket()
sock.connect((self.test_address, self.test_port))
self.client = SAPNIStreamSocket(sock)
packet = self.client.sr(self.test_string)
packet.decode_payload_as(Raw)
self.client.close()
self.assertIn(SAPNI, packet)
self.assertEqual(packet[SAPNI].length, len(self.test_string))
self.assertEqual(packet.payload.load, self.test_string)
self.stop_server()
def test_sapnistreamsocket_base_cls(self):
"""Test SAPNIStreamSocket handling of custom base packet classes"""
self.start_server(SAPNITestHandler)
class SomeClass(Packet):
fields_desc = [StrField("text", None)]
sock = socket.socket()
sock.connect((self.test_address, self.test_port))
self.client = SAPNIStreamSocket(sock, base_cls=SomeClass)
packet = self.client.sr(self.test_string)
self.client.close()
self.assertIn(SAPNI, packet)
self.assertIn(SomeClass, packet)
self.assertEqual(packet[SAPNI].length, len(self.test_string))
self.assertEqual(packet[SomeClass].text, self.test_string)
self.stop_server()
def test_sapnistreamsocket_getnisocket(self):
"""Test SAPNIStreamSocket get nisocket class method"""
self.start_server(SAPNITestHandler)
self.client = SAPNIStreamSocket.get_nisocket(self.test_address,
self.test_port)
packet = self.client.sr(self.test_string)
packet.decode_payload_as(Raw)
self.client.close()
self.assertIn(SAPNI, packet)
self.assertEqual(packet[SAPNI].length, len(self.test_string))
self.assertEqual(packet.payload.load, self.test_string)
self.stop_server()
def test_sapnistreamsocket_without_keep_alive(self):
"""Test SAPNIStreamSocket without keep alive"""
self.start_server(SAPNITestHandlerKeepAlive)
sock = socket.socket()
sock.connect((self.test_address, self.test_port))
self.client = SAPNIStreamSocket(sock, keep_alive=False)
packet = self.client.sr(self.test_string)
packet.decode_payload_as(Raw)
self.client.close()
# We should receive a PING instead of our packet
self.assertIn(SAPNI, packet)
self.assertEqual(packet[SAPNI].length, len(SAPNI.SAPNI_PING))
self.assertEqual(packet.payload.load, SAPNI.SAPNI_PING)
self.stop_server()
def test_sapnistreamsocket_with_keep_alive(self):
"""Test SAPNIStreamSocket with keep alive"""
self.start_server(SAPNITestHandlerKeepAlive)
#.........这里部分代码省略.........
示例8: LoggerServer
# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import shutdown [as 别名]
class LoggerServer(threading.Thread):
def __init__(self):
super(LoggerServer, self).__init__()
self.server = None
self.mh = None
self.omh = None
def run(self):
# check logger path
logger_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), constants.LOGGER_DIR)
logger_online_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), constants.LOGGER_ONLINE_DIR)
if os.path.exists(logger_path):
if not os.path.isdir(logger_path):
os.remove(logger_path)
os.mkdir(logger_path)
else:
os.mkdir(logger_path)
logger_file = os.path.join(logger_path, constants.LOGGER_FILE)
logger_online_file = os.path.join(logger_online_path, constants.LOGGER_ONLINE_FILE)
# rotating file handler
#rh = logging.handlers.RotatingFileHandler(logger_file,
# maxBytes = constants.LOGGER_FILE_MAX_BYTE,
# backupCount = constants.LOGGER_FILE_BACKUP_COUNT)
#rh.setLevel(logging.DEBUG)
# RotatingFileHandler failed sometimes because of os.rename(), so change to FileHandler, and maintain the log files manually
if os.path.exists(logger_file):
logger_file_stat = os.stat(logger_file)
if logger_file_stat.st_size > constants.LOGGER_FILE_MAX_BYTE:
file_list = os.listdir(logger_path)
for count in range(constants.LOGGER_FILE_BACKUP_COUNT, 0, -1):
if os.path.exists('%s.%s' % (logger_file, count)):
if count == constants.LOGGER_FILE_BACKUP_COUNT:
os.remove('%s.%s' % (logger_file, count))
else:
os.rename('%s.%s' % (logger_file, count), '%s.%s' % (logger_file, count + 1))
os.rename(logger_file, '%s.%s' % (logger_file, str(1)))
fh = logging.FileHandler(logger_file)
fh.setLevel(logging.DEBUG)
ofh = logging.FileHandler(logger_online_file)
ofh.setLevel(logging.DEBUG)
# memory handler for rotate file handler
self.mh = logging.handlers.MemoryHandler(constants.LOGGER_FILE_MEMORY_CACHE, target = fh)
self.mh.setLevel(logging.DEBUG)
self.omh = logging.handlers.MemoryHandler(constants.LOGGER_FILE_MEMORY_CACHE, target = ofh)
self.omh.setLevel(logging.DEBUG)
# set logger format
formatter = logging.Formatter("%(asctime)s - %(name)s - %(filename)s[line:%(lineno)d] - %(levelname)s - %(message)s")
#rh.setFormatter(formatter)
fh.setFormatter(formatter)
ofh.setFormatter(formatter)
self.mh.setFormatter(formatter)
self.omh.setFormatter(formatter)
# main logger
logger = logging.getLogger(constants.LOGGER_SERVER_NAME)
logger.propagate = 0
logger.setLevel(logging.DEBUG)
# add handle to logger
logger.addHandler(self.mh)
logger.addHandler(self.omh)
self.server = ThreadingTCPServer((constants.LOGGER_SERVER_IP, constants.LOGGER_SERVER_PORT), LogRequestHandler)
self.server.serve_forever()
self.server.server_close()
def stop_server(self):
if self.mh:
self.mh.flush()
if self.omh:
self.omh.flush()
if self.server:
self.server.shutdown()
def get_server(self):
return self.server
示例9: ThreadingTCPServer
# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import shutdown [as 别名]
while True:
self.data = self.request.recv(1024)
if not self.data: break
gLock.acquire()
print "Server received {0} bytes on thread {1} from {2}:{3}".format(len(self.data),
threading.current_thread().name, *self.client_address)
print " {0}".format(self.data)
gLock.release()
self.request.send(self.data)
try:
s = ThreadingTCPServer((server_addr, server_port), EchoHandler)
s.allow_reuse_address = True
print "Server started"
s.serve_forever()
except (KeyboardInterrupt, SystemExit):
pass
finally:
s.shutdown()
print "Server stopped"
示例10: ServerThread
# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import shutdown [as 别名]
#.........这里部分代码省略.........
data = self.request.recv(1024)
if not data:
logger.debug('self.request.recv null')
return
try:
data = json.loads(data)
search_url = data['url']
except:
logger.debug('data error')
return
try:
self.req_num = self.get_num()
logger.debug('[begin]req_num: %d, search url: %s' % (self.req_num, search_url))
except Exception as e:
logger.debug('search url: %s, error %s' % (search_url, str(e)))
logger.exception(e)
try:
#extraInfo = data['extraInfo']
if not search_url:
logger.debug('search_url null')
logger.debug('[end]req_num: %d, search url: %s' % (self.req_num, search_url))
self.request.send('')
else:
link_url_list = Search().search(search_url)
logger.debug('req_num: %d, google搜索返回的记录数 %d' % (self.req_num, len(link_url_list)))
link_url_list = ['http:' + l if l[:2] == '//' else l for l in link_url_list]
#link_url_list = [(l, req_num) for l in link_url_list]
_results = []
for l in link_url_list:
list_url = self.get_page_link_list(l)
if config.crawl_level == 2:
result = reduce(lambda _list, elem: _list.extend(elem) or _list, pool.map(self.get_page_link_list, [l[0] for l in list_url]), list())
for j in list_url:
result.append((j[0], l))
_results.extend(result)
else:
for j in list_url:
_results.append((j[0], l))
d = {}
d['request'] = data
d['response'] = _results
#with open('out.json', 'w+') as f:
for l in link_url_list:
d['response'].append((l, search_url))
logger.debug('[end]req_num: %d, search url: %s, 搜索到的记录数 %d' % (self.req_num, search_url, len(d['response'])))
out_str = json.dumps(d)
self.request.send(out_str)
except Exception as e:
logger.debug('[end]req_num: %d, search url: %s, error %s' % (self.req_num, search_url, str(e)))
logger.exception(e)
# def send_response(self, res):
# logger.debug('send_response: %d' % len(res))
# try:
# connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
# channel = connection.channel()
# channel.queue_declare(queue='google_search')
# channel.basic_publish(exchange='', routing_key='google_search', body=res)
# connection.close()
# except Exception as e:
# logger.error(str(e))
# logger.exception(e)
class DebugThread(threading.Thread):
def __init__(self, pool):
super(type(self), self).__init__()
self.pool = pool
self.setDaemon(True)
self.start()
def run(self):
logger.info('debug thread start!!!')
while True:
logger.info('pool state %d' % self.pool._state)
for l in self.pool._pool:
print l.is_alive(), l.name
print len(self.pool._pool)
time.sleep(5)
def run(self):
logger.info('server thread start!!!')
try:
self.server = ThreadingTCPServer(('127.0.0.1', 50005), self.Handler)
self.server.pool = ThreadPool(config.thread_num)
#self.DebugThread(self.server.pool)
self.server.serve_forever()
except Exception as e:
logger.error(str(e) + ' 具体栈回溯信息查看crit.log ')
logger.exception(e)
def shut_down(self):
self.server.shutdown()
示例11: shutdown
# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import shutdown [as 别名]
def shutdown(self, args):
""" Overloaded method that kills the correlator before shutdown."""
if self._started:
self.stop_correlator('')
ThreadingTCPServer.shutdown(self)
return SBYTE.pack(0)