本文整理汇总了Python中socketserver.TCPServer方法的典型用法代码示例。如果您正苦于以下问题:Python socketserver.TCPServer方法的具体用法?Python socketserver.TCPServer怎么用?Python socketserver.TCPServer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socketserver
的用法示例。
在下文中一共展示了socketserver.TCPServer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_feed_server
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import TCPServer [as 别名]
def run_feed_server():
#stands up the feed server, points to the CB/json_feeds dir
chdir('data/json_feeds/')
port = 8000
handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(("", port), handler)
try:
print((Fore.GREEN + '\n[+]' + Fore.RESET), end=' ')
print(('Feed Server listening at http://%s:8000' % gethostname()))
httpd.serve_forever()
except:
print((Fore.RED + '\n[-]' + Fore.RESET), end=' ')
print("Server exited")
return
示例2: start_serving
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import TCPServer [as 别名]
def start_serving(self,host="0.0.0.0"):
serve_dir = os.path.join(Settings.path,"core","www",self.name)
f = open( os.path.join(serve_dir,"index.html"),"w")
f.write(self.html)
f.close()
class ReusableTCPServer(socketserver.TCPServer):
allow_reuse_address = True
logging = False
class MyHandler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=serve_dir, **kwargs)
def log_message(self, format, *args):
if self.server.logging:
http.server.SimpleHTTPRequestHandler.log_message(self, format, *args)
self.httpd = ReusableTCPServer( (host, self.port), MyHandler)
t = thread.start_new_thread(self.httpd.serve_forever, ())
示例3: _run_webserver
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import TCPServer [as 别名]
def _run_webserver(self):
"""
Starts the webserver on the given port.
"""
try:
os.chdir('./editor/dist')
self.httpd = TCPServer(('', self.port), QuietHTTPRequestHandler)
print("Editor started at port %d" % self.port)
url = 'http://localhost:%d/dettect-editor' % self.port
if not os.getenv('DeTTECT_DOCKER_CONTAINER'):
print("Opening webbrowser: " + url)
webbrowser.open_new_tab(url)
else:
print("You can open the Editor on: " + url)
self.httpd.serve_forever()
except Exception as e:
print("Could not start webserver: " + str(e))
示例4: test_shutdown
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import TCPServer [as 别名]
def test_shutdown(self):
# Issue #2302: shutdown() should always succeed in making an
# other thread leave serve_forever().
class MyServer(socketserver.TCPServer):
pass
class MyHandler(socketserver.StreamRequestHandler):
pass
threads = []
for i in range(20):
s = MyServer((HOST, 0), MyHandler)
t = threading.Thread(
name='MyServer serving',
target=s.serve_forever,
kwargs={'poll_interval':0.01})
t.daemon = True # In case this function raises.
threads.append((t, s))
for t, s in threads:
t.start()
s.shutdown()
for t, s in threads:
t.join()
s.server_close()
示例5: handle_error
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import TCPServer [as 别名]
def handle_error(self, request, client_address):
"""Override TCPServer method
Error message goes to __stderr__. No error message if exiting
normally or socket raised EOF. Other exceptions not handled in
server code will cause os._exit.
"""
try:
raise
except SystemExit:
raise
except:
erf = sys.__stderr__
print('\n' + '-'*40, file=erf)
print('Unhandled server exception!', file=erf)
print('Thread: %s' % threading.current_thread().name, file=erf)
print('Client Address: ', client_address, file=erf)
print('Request: ', repr(request), file=erf)
traceback.print_exc(file=erf)
print('\n*** Unrecoverable, server exiting!', file=erf)
print('-'*40, file=erf)
os._exit(0)
#----------------- end class RPCServer --------------------
示例6: enqueue_lines
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import TCPServer [as 别名]
def enqueue_lines(self):
run = True
line_queue = self.line_queue
class WebSocketHandler(websocket.BaseWebSocketHandler):
def on_message(self, message):
line_queue.put(message)
def should_close(self):
return not run
class _TCPServer(TCPServer):
allow_reuse_address = True
self.server = _TCPServer(self.addr_port, WebSocketHandler)
try:
self.server.serve_forever()
finally:
run = False
示例7: testImportHttpFile
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import TCPServer [as 别名]
def testImportHttpFile(self):
'''Test importing a file over HTTP.'''
http = socketserver.TCPServer(('', 0), StubHttpRequestHandler)
try:
httpThread = threading.Thread(target=http.serve_forever)
httpThread.start()
try:
assignedPort = http.server_address[1]
url = 'http://localhost:%d' % assignedPort
with self.jobstore_initialized.readFileStream(
self.jobstore_initialized.importFile(url)) as readable:
f1 = readable.read()
f2 = StubHttpRequestHandler.fileContents
if isinstance(f1, bytes) and not isinstance(f2, bytes):
f1 = f1.decode()
if isinstance(f2, bytes) and not isinstance(f1, bytes):
f1 = f1.encode()
self.assertEqual(f1, f2)
finally:
http.shutdown()
httpThread.join()
finally:
http.server_close()
示例8: setUp
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import TCPServer [as 别名]
def setUp(self):
self.registry = CollectorRegistry()
self.data = ''
class TCPHandler(SocketServer.BaseRequestHandler):
def handle(s):
self.data = s.request.recv(1024)
server = SocketServer.TCPServer(('', 0), TCPHandler)
class ServingThread(threading.Thread):
def run(self):
server.handle_request()
server.socket.close()
self.t = ServingThread()
self.t.start()
# Explicitly use localhost as the target host, since connecting to 0.0.0.0 fails on Windows
address = ('localhost', server.server_address[1])
self.gb = GraphiteBridge(address, self.registry, _timer=fake_timer)
示例9: __init__
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import TCPServer [as 别名]
def __init__(self, args=None):
"""
:param args: Argument parser args. If None, the server will setup and
use its own argument parser (using
:meth:`pyqode.core.backend.default_parser`)
"""
self.reset_heartbeat()
if not args:
args = default_parser().parse_args()
self.port = args.port
self.timeout = HEARTBEAT_DELAY
self._Handler.srv = self
socketserver.TCPServer.__init__(
self, ('127.0.0.1', int(args.port)), self._Handler)
print('started on 127.0.0.1:%d' % int(args.port))
print('running with python %d.%d.%d' % (sys.version_info[:3]))
self._heartbeat_thread = threading.Thread(target=self.heartbeat)
self._heartbeat_thread.setDaemon(True)
self._heartbeat_thread.start()
示例10: __init__
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import TCPServer [as 别名]
def __init__(self, selector, listen_address, clients, known_devices):
self.clients = clients
if isinstance(listen_address, int) or listen_address.isnumeric():
s_addr = ('localhost', int(listen_address))
socketserver.TCPServer.allow_reuse_address = True
s = socketserver.TCPServer(s_addr, ApiRequestHandler)
else:
s = socketserver.UnixStreamServer(
listen_address, ApiRequestHandler)
# quiet hacky
s.wsd_clients = clients
s.wsd_known_devices = known_devices
selector.register(s.fileno(), selectors.EVENT_READ, s)
示例11: __init__
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import TCPServer [as 别名]
def __init__(self, layout, port=3142, interface='0.0.0.0'):
self.layout = layout
self.address = (interface, port)
SocketServer.TCPServer.allow_reuse_address = True
self._server = ThreadedDataServer(self.address, ThreadedDataHandler)
self._server.update = self.update
self._server.set_brightness = self.layout.set_brightness
示例12: main
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import TCPServer [as 别名]
def main():
HOST, PORT = "0.0.0.0", 5555
socketserver.TCPServer.allow_reuse_address = True
with socketserver.TCPServer((HOST, PORT), MyTCPHandler) as server:
server.serve_forever()
示例13: __init__
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import TCPServer [as 别名]
def __init__(self, server_address, translation_server_config, text_to_sentences_splitter, log):
self.manager = Manager(translation_server_config, text_to_sentences_splitter, log)
handler_class = self.make_request_handler(self.manager, log)
socketserver.TCPServer.__init__(self, server_address, handler_class)
示例14: main
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import TCPServer [as 别名]
def main():
print("To close connection enter 'EXIT'")
print("The computer may be stalled by some commands, just try again")
print()
with socketserver.TCPServer(("0.0.0.0", PORT), CmdHttpHandler) as server:
server.serve_forever()
示例15: setUpClass
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import TCPServer [as 别名]
def setUpClass(cls):
global web_server
# create a simple web server listening on localhost
class _customTCPServer(socketserver.TCPServer):
allow_reuse_address = True
web_server = _customTCPServer(('', LOCAL_PORT), http.server.SimpleHTTPRequestHandler)
web_server_thread = threading.Thread(target=web_server.serve_forever)
web_server_thread.daemon = True
web_server_thread.start()