def launch(path): """Start a server to view an exported D3PO bundle, and open a browser. :param path: The TLD of the bundle """ from SocketServer import TCPServer from SimpleHTTPServer import SimpleHTTPRequestHandler from random import randrange from socket import error import webbrowser from threading import Thread os.chdir(path) while True: try: PORT = randrange(8000, 9000) server = TCPServer(("", PORT), SimpleHTTPRequestHandler, False) server.allow_reuse_address = True server.server_bind() break except error: # port already taken pass print('Serving D3PO on port 0.0.0.0:%i' % PORT) server.server_activate() thread = Thread(target=server.serve_forever) thread.setDaemon(True) # do not prevent shutdown thread.start() webbrowser.open('http://0.0.0.0:%i' % PORT)
def launch(path): """Start a server to view an exported D3PO bundle, and open a browser. :param path: The TLD of the bundle """ from SocketServer import TCPServer from SimpleHTTPServer import SimpleHTTPRequestHandler from random import randrange from socket import error import webbrowser from threading import Thread os.chdir(path) while True: try: PORT = randrange(8000, 9000) server = TCPServer(("", PORT), SimpleHTTPRequestHandler, False) server.allow_reuse_address = True server.server_bind() break except error: # port already taken pass print('Serving D3PO on port 0.0.0.0:%i' % PORT) server.server_activate() thread = Thread(target=server.serve_forever) thread.setDaemon(True) # do not prevent shutdown thread.start() webbrowser.open('http://0.0.0.0:%i' % PORT)
def _start_web_server(): global http_server try: from SimpleHTTPServer import SimpleHTTPRequestHandler from SocketServer import TCPServer except ImportError: from http.server import SimpleHTTPRequestHandler from socketserver import TCPServer try: handler = SimpleHTTPRequestHandler handler.directory = os.path.join( os.path.dirname(__file__), "..", "..") http_server = TCPServer( ("", 8000), handler, bind_and_activate=False) http_server.daemon_threads = True http_server.allow_reuse_address = True http_server.server_bind() http_server.server_activate() http_server_ready.set() http_server.serve_forever() except: import traceback traceback.print_exc() finally: http_server = None http_server_ready.set()
def setUp(self): handler = SimpleHTTPRequestHandler httpd = TCPServer(("", PORT), handler) httpd.allow_reuse_address = True self.httpd_thread = Thread(target=httpd.serve_forever) self.httpd_thread.setDaemon(True) self.httpd_thread.start()
def run_server(port=19835): serv = TCPServer(('', port), Handler) serv.allow_reuse_address = True try: serv.serve_forever() except KeyboardInterrupt: print 'interrupted...' finally: serv.socket.close()
def _documentation_server(doc_root, port): from SimpleHTTPServer import SimpleHTTPRequestHandler from SocketServer import TCPServer os.chdir(doc_root) httpd = TCPServer(("127.0.0.1", port), SimpleHTTPRequestHandler, bind_and_activate=False) httpd.allow_reuse_address = True try: httpd.server_bind() httpd.server_activate() httpd.serve_forever() finally: httpd.server_close()
def test(self, selftest): # We need to discover SERVEP_IP and set up SERVER_PORT # Note: Port 7 is Echo Protocol: # # Port number rationale: # # The Echo Protocol is a service in the Internet Protocol Suite defined # in RFC 862. It was originally proposed for testing and measurement # of round-trip times[citation needed] in IP networks. # # A host may connect to a server that supports the Echo Protocol using # the Transmission Control Protocol (TCP) or the User Datagram Protocol # (UDP) on the well-known port number 7. The server sends back an # identical copy of the data it received. SERVER_IP = str(socket.gethostbyname(socket.getfqdn())) SERVER_PORT = 32765 # Returning none will suppress host test from printing success code server = TCPServer((SERVER_IP, SERVER_PORT), TCPEchoClient_Handler) server.allow_reuse_address = True print "HOST: Listening for TCP connections: " + SERVER_IP + ":" + str(SERVER_PORT) self.send_server_ip_port(selftest, SERVER_IP, SERVER_PORT) server.serve_forever()
def test(self, selftest): # We need to discover SERVEP_IP and set up SERVER_PORT # Note: Port 7 is Echo Protocol: # # Port number rationale: # # The Echo Protocol is a service in the Internet Protocol Suite defined # in RFC 862. It was originally proposed for testing and measurement # of round-trip times[citation needed] in IP networks. # # A host may connect to a server that supports the Echo Protocol using # the Transmission Control Protocol (TCP) or the User Datagram Protocol # (UDP) on the well-known port number 7. The server sends back an # identical copy of the data it received. SERVER_IP = str(socket.gethostbyname(socket.getfqdn())) SERVER_PORT = 32765 # Returning none will suppress host test from printing success code server = TCPServer((SERVER_IP, SERVER_PORT), TCPEchoClient_Handler) server.allow_reuse_address = True print "HOST: Listening for TCP connections: " + SERVER_IP + ":" + str( SERVER_PORT) self.send_server_ip_port(selftest, SERVER_IP, SERVER_PORT) server.serve_forever()
level=logging.DEBUG) logging.info(platform.python_version()) logging.debug(sys.argv) port = int(sys.argv[1]) app_id = sys.argv[2] version = sys.argv[3] base_url = sys.argv[4] task_id = os.getenv("MESOS_TASK_ID", "<UNKNOWN>") # Defer binding and activating the server to a later point, allowing to set # allow_reuse_address=True option. httpd = HTTPServer(("", port), make_handler(app_id, version, task_id, base_url), bind_and_activate=False) httpd.allow_reuse_address = True msg = "AppMock[%s %s]: %s has taken the stage at port %d. "\ "Will query %s for health and readiness status." logging.info(msg, app_id, version, task_id, port, base_url) # Trigger proper shutdown on SIGTERM. def handle_sigterm(signum, frame): logging.warning( "Received {} signal. Closing the server...".format(signum)) httpd.server_close() signal.signal(signal.SIGTERM, handle_sigterm) try: httpd.server_bind()
#!/usr/bin/env python from SocketServer import BaseRequestHandler as BRH, TCPServer as TCP from time import ctime HOST = '' PORT = 21567 BUFSIZ = 1024 ADDR = (HOST, PORT) class MyRequestHandler(BRH): def handle(self): print '...connected from:', self.client_address self.request.send('[%s] %s' % (ctime(), self.request.recv(BUFSIZ))) tcpServ = TCP(ADDR, MyRequestHandler) tcpServ.allow_reuse_address = True print 'waiting for connection...' tcpServ.serve_forever()
#!/usr/bin/env python from SocketServer import (TCPServer as TCP, StreamRequestHandler as SRH) from time import ctime HOST = '' PORT = 21567 ADDR = (HOST, PORT) class MyRequestHandler(SRH): def handle(self): print '...connected from:', self.client_address self.wfile.write('[%s] %s' % (ctime(), self.rfile.readline())) tcpServ = TCP(ADDR, MyRequestHandler) tcpServ.allow_reuse_address = True print 'waiting for connection...' tcpServ.serve_forever()
level=logging.DEBUG) logging.info(platform.python_version()) logging.debug(sys.argv) port = int(sys.argv[1]) app_id = sys.argv[2] version = sys.argv[3] base_url = sys.argv[4] task_id = os.getenv("MESOS_TASK_ID", "<UNKNOWN>") # Defer binding and activating the server to a later point, allowing to set # allow_reuse_address=True option. httpd = HTTPServer(("", port), make_handler(app_id, version, task_id, base_url), bind_and_activate=False) httpd.allow_reuse_address = True msg = "AppMock[%s %s]: %s has taken the stage at port %d. "\ "Will query %s for health and readiness status." logging.info(msg, app_id, version, task_id, port, base_url) # Trigger proper shutdown on SIGTERM. def handle_sigterm(signum, frame): logging.warning("Received {} signal. Closing the server...".format(signum)) httpd.server_close() signal.signal(signal.SIGTERM, handle_sigterm) try: httpd.server_bind() httpd.server_activate()
from SocketServer import TCPServer, StreamRequestHandler class SimpleHandler(StreamRequestHandler): def handle(self): addr = self.request.getpeername() print 'Got connection from: ' + addr[0] self.wfile.write('Server is constructed using SocketServer\n') server = TCPServer(('', 9606), SimpleHandler) server.allow_reuse_address = True server.serve_forever()