本文整理匯總了Python中socketserver.ThreadingTCPServer方法的典型用法代碼示例。如果您正苦於以下問題:Python socketserver.ThreadingTCPServer方法的具體用法?Python socketserver.ThreadingTCPServer怎麽用?Python socketserver.ThreadingTCPServer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類socketserver
的用法示例。
在下文中一共展示了socketserver.ThreadingTCPServer方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: start_viewer_server
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import ThreadingTCPServer [as 別名]
def start_viewer_server(port: int, game: Game) -> socketserver.BaseServer:
'''
Start a socket server for the players to connect to
Args:
port: port to connect to viewer on
game: The game information that is being run
use_docker bool: whether to use docker or not
Return:
server_thread: The connection so it can be closed by parent functions at
the appropriate time
'''
# Create handler for mangaing each connections to server
receive_handler = create_receive_handler(game, {}, False, False)
# Start server
server = socketserver.ThreadingTCPServer(('localhost', port), receive_handler)
server_thread = threading.Thread(target=server.serve_forever, daemon=True)
server_thread.start()
return server
示例2: main
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import ThreadingTCPServer [as 別名]
def main():
HOST, PORT = "localhost", 6030
overpass_thread = threading.Thread(target=process_queue_loop)
overpass_thread.daemon = True
overpass_thread.start()
socketserver.ThreadingTCPServer.allow_reuse_address = True
server = socketserver.ThreadingTCPServer((HOST, PORT), RequestHandler)
ip, port = server.server_address
server_thread = threading.Thread(target=server.serve_forever)
server_thread.name = 'server thread'
server_thread.start()
print("Server loop running in thread:", server_thread.name)
server_thread.join()
示例3: run
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import ThreadingTCPServer [as 別名]
def run():
# create a genesis block
t = str(datetime.now())
genesis_block = {
"Index": 0,
"Timestamp": t,
"BPM": 0,
"PrevHash": "",
"Validator": ""
}
genesis_block["Hash"] = calculate_hash(genesis_block)
print(genesis_block)
block_chain.append(genesis_block)
thread_canditate = threading.Thread(target=candidate, args=(candidate_blocks,), daemon=True)
thread_pick = threading.Thread(target=pick_winner, args=(announcements,), daemon=True)
thread_canditate.start()
thread_pick.start()
# start a tcp server
serv = ThreadingTCPServer(('', 9090), HandleConn)
serv.serve_forever()
示例4: main
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import ThreadingTCPServer [as 別名]
def main():
example = 'Example:\n\n$ python3 server.py -U msf -P msf -v -s -l 4444'
args = ArgumentParser(prog='Cooolis-ms',epilog=example)
args.add_argument('-U','--username',help='Metasploit web service username',required=True)
args.add_argument('-P','--password',help='Metasploit web service password',required=True)
args.add_argument('-H','--host',help='Metasploit web service host, Default: localhost',default='localhost')
args.add_argument('-p','--port',help='Metasploit RPC service port, Default: 55553',default=55553,type=int)
args.add_argument('-S','--server',help='Payload sender listen host, Default: localhost',default='localhost')
args.add_argument('-l','--listen',help='Payload listen port, Default: 1111',default=1111,type=int)
args.add_argument('-u','--uri',help='Metasploit RPC service uri, Default: /api/1.0/',default='/api/1.0/')
args.add_argument('-t','--type',help='Payload Type',choices=('exe','ruby','c','dll','vbs','powershell'))
args.add_argument('-s','--ssl',help='Enable ssl, Default: True',action="store_true",default=True)
args.add_argument('-v','--versobe',help='Enable debug',action="store_true")
parser = args.parse_args()
print("[*]Server Host : {host} , Server Port : {port}".format(host=parser.server,port=parser.listen))
server = ThreadingTCPServer((parser.server,parser.listen),Metasploit_RPC.Creator(parser))
server.serve_forever()
示例5: __init__
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import ThreadingTCPServer [as 別名]
def __init__(self, remote_server, ssh_transport, *args, **kwargs):
self.remote_server = remote_server
self.ssh_transport = ssh_transport
socketserver.ThreadingTCPServer.__init__(self, *args, **kwargs)
示例6: setUp
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import ThreadingTCPServer [as 別名]
def setUp(self):
super().setUp()
self.httpd = socketserver.ThreadingTCPServer(("localhost", 0), createHttpHandler(self.repo.name))
self.ip, self.port = self.httpd.server_address
self.server = threading.Thread(target=self.httpd.serve_forever)
self.server.daemon = True
self.server.start()
示例7: serve_listen
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import ThreadingTCPServer [as 別名]
def serve_listen():
server = ThreadingTCPServer((HOST, PORT), Handler)
server.serve_forever()
print(server)
# 主函數
示例8: run
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import ThreadingTCPServer [as 別名]
def run(self):
_ClientHandler.log_info("cht_proxy_daemon start as proxy-server:'{0}';port:'{1}'".format(self._ip_address, self._port_number))
_ClientHandler.log_info("logfile:'{0}'".format(self._logfile))
_serialdevice_initialised=[]
for devicename in self.devicename_keys():
if self.devicename_initflag(devicename) == 0:
#check for already initialised serial device, if not then start transceiver_if
serialdevice = self.transceiver_serialdevice(devicename)
if not serialdevice in (_serialdevice_initialised):
baudrate = self.transceiver_baudrate(devicename)
devicetype = self.transceiver_devicetype(devicename)
#start transceiver-if for that serial device
transceiver_if = cht_transceiver_if(serialdevice, baudrate, devicetype)
#add used serial-device to list
_serialdevice_initialised.append(serialdevice)
#add transceiver to list
self._ht_transceiver_if.append(transceiver_if)
transceiver_if.setDaemon(True)
transceiver_if.start()
#set initialise-flag for devicename
self.devicename_initflag(devicename, 1)
try:
self._server=socketserver.ThreadingTCPServer((self._ip_address, self._port_number), cht_RequestHandler)
self._server.serve_forever()
_ClientHandler.log_critical("cht_proxy_daemon terminated")
_ClientHandler.log_info("---------------------------")
raise
except:
_ClientHandler.log_critical("cht_proxy_daemon terminated")
_ClientHandler.log_info("---------------------------")
raise
示例9: main
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import ThreadingTCPServer [as 別名]
def main():
import argparse
global trace_file, TARGET_HOST, TARGET_PORT
HOST, PORT = "localhost", 8090
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--trace_file', type=argparse.FileType('w'))
parser.add_argument('-b', '--bind', default=HOST)
parser.add_argument('-p', '--port', type=int, default=PORT)
parser.add_argument('-n', '--negotiate', help='Negotiate with the given server name')
parser.add_argument('TARGET_HOST')
parser.add_argument('TARGET_PORT', type=int)
args = parser.parse_args()
TARGET_HOST = args.TARGET_HOST
TARGET_PORT = args.TARGET_PORT
trace_file = args.trace_file
register_types()
NETTCPProxy.negotiate = bool(args.negotiate)
NETTCPProxy.server_name = args.negotiate
if GSSAPIStream is None and NETTCPProxy.negotiate:
log.error("GSSAPI not available, negotiation not possible. Try python2 with gssapi")
sys.exit(1)
server = SocketServer.ThreadingTCPServer((args.bind, args.port), NETTCPProxy)
server.serve_forever()
示例10: test_ThreadingTCPServer
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import ThreadingTCPServer [as 別名]
def test_ThreadingTCPServer(self):
self.run_server(socketserver.ThreadingTCPServer,
socketserver.StreamRequestHandler,
self.stream_examine)
示例11: start
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import ThreadingTCPServer [as 別名]
def start(self):
server = socketserver.ThreadingTCPServer(
(self.host, self.port), _PeerRequestHandler)
server.peer = self
try:
server.serve_forever()
except KeyboardInterrupt as _:
server.server_close()
示例12: main
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import ThreadingTCPServer [as 別名]
def main():
print('Starting')
address = ('localhost', 8080)
server = socketserver.ThreadingTCPServer(address,
MyTCPHandler)
print('Activating server')
server.serve_forever()
示例13: __init__
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import ThreadingTCPServer [as 別名]
def __init__(self, addr, requestHandler=RPCHandler, bind_and_activate=True):
self.instance = None
socketserver.ThreadingTCPServer.__init__(self, addr, requestHandler, bind_and_activate)
self.previous_method = None
self.same_method_call_count = 0
self.process = psutil.Process(os.getpid())
示例14: __init__
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import ThreadingTCPServer [as 別名]
def __init__(self, port=8000):
self._server_address = ("127.0.0.1", port)
self._handler = SimpleHTTPRequestHandlerHere
self.httpd = ThreadingTCPServer(
self._server_address, self._handler, bind_and_activate=False
)
示例15: handle
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import ThreadingTCPServer [as 別名]
def handle(self):
"""重載handle方法"""
print(self.client_address, '連接了服務器')
request_data = self.request.recv(2048).decode('utf-8')
request_header_lines = request_data.splitlines()
# print(request_header_lines[0]) # 第一行為請求頭信息
# 解析請求頭,獲取具體請求信息
pattern = r'[^/]+(/[^ ]*)'
request_html_name = re.match(pattern, request_header_lines[0]).group(1)
# 根據解析到的內容補全將要讀取文件的路徑
if request_html_name == '/':
request_html_name = STATIC_PATH + 'baidu.html'
else:
request_html_name = STATIC_PATH + request_html_name
# 根據文件情況來返回相應的信息
try:
html_file = open(request_html_name, 'rb')
except FileNotFoundError:
# 文件不存在,則返回文件不存在,並返回狀態碼404
resp_headers = 'HTTP/1.1 404 not found\r\n'
resp_headers += "Server: PWB" + str(VERSION) + '\r\n'
resp_headers += '\r\n'
resp_body = '==== 404 file not found===='.encode('utf-8')
else:
# 文件存在,則讀取文件內容,並返回狀態碼200
resp_headers = "HTTP/1.1 200 OK\r\n" # 200代表響應成功並找到資源
resp_headers += "Server: PWB" + str(VERSION) + '\r\n' # 告訴瀏覽器服務器
resp_headers += '\r\n' # 空行隔開body
resp_body = html_file.read() # 顯示內容為讀取的文件內容
html_file.close()
finally:
resp_data = resp_headers.encode('utf-8') + resp_body # 結合響應頭和響應體
# 發送相應數據至瀏覽器
self.request.send(resp_data)
self.request.close() # HTTP短連接,請求完即關閉TCP連接