Python SocketServer.ThreadingMixIn() Examples

The following are 6 code examples of SocketServer.ThreadingMixIn(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module SocketServer , or try the search function .
Example #1
Source File: testrun.py    From honeypot with GNU General Public License v2.0 8 votes vote down vote up
def run_tcp(realport, fakeport, handler):
	class SingleTCPHandler(SocketServer.BaseRequestHandler):
		def handle(self):
			srcaddr, srcport = self.request.getpeername()
			print("Connection from {}:{}".format(srcaddr, srcport))
			handler(self.request, fakeport)

	class SimpleServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
		daemon_threads = True
		allow_reuse_address = True

		def __init__(self, server_address, RequestHandlerClass):
			SocketServer.TCPServer.__init__(self, server_address, RequestHandlerClass)

	server = SimpleServer(('127.0.0.1', realport), SingleTCPHandler)
	try:
		server.serve_forever()
	except KeyboardInterrupt:
		sys.exit(0) 
Example #2
Source File: testrun.py    From honeypot with GNU General Public License v2.0 6 votes vote down vote up
def run_udp(realport, fakeport, handler):
	class SingleUDPHandler(SocketServer.BaseRequestHandler):
		def handle(self):
			srcaddr, srcport = self.client_address
			print("Packet from {}:{}".format(srcaddr, srcport))
			handler(self.request[1], self.request[0], self.client_address, fakeport)

	class SimpleServer(SocketServer.ThreadingMixIn, SocketServer.UDPServer):
		daemon_threads = True

		def __init__(self, server_address, RequestHandlerClass):
			SocketServer.UDPServer.__init__(self, server_address, RequestHandlerClass)

	server = SimpleServer(('127.0.0.1', realport), SingleUDPHandler)
	try:
		server.serve_forever()
	except KeyboardInterrupt:
		sys.exit(0) 
Example #3
Source File: plugin_listener.py    From deen with Apache License 2.0 6 votes vote down vote up
def _http_python2(self, listen_ssl=False):
        """Listen for HTTP connections with Python 2."""
        class ThreadingSimpleServer(SocketServer.ThreadingMixIn,
                                    BaseHTTPServer.HTTPServer):
            pass
        server = ThreadingSimpleServer(self.listen_socket,
                                       DeenHTTPRequestHandler)
        os.chdir(self.serving_directory)
        message = 'Serving HTTP at port ' + str(self.listen_port)
        if listen_ssl:
            message += ' (SSL)'
        print(message)
        try:
            while 1:
                sys.stdout.flush()
                server.handle_request()
        except KeyboardInterrupt:
            server.socket.close() 
Example #4
Source File: test_socket_jy.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def start_server():
    server_address = ('127.0.0.1', 0)

    class DaemonThreadingMixIn(ThreadingMixIn):
        daemon_threads = True

    class ThreadedHTTPServer(DaemonThreadingMixIn, HTTPServer):
        """Handle requests in a separate thread."""

    # not actually going to do anything with this server, so a
    # do-nothing handler is reasonable
    httpd = ThreadedHTTPServer(server_address, BaseHTTPRequestHandler)
    server_thread = threading.Thread(target=httpd.serve_forever)
    server_thread.daemon = True
    server_thread.start()
    return httpd, server_thread 
Example #5
Source File: test_socket_jy.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
def start_server():
    server_address = ('127.0.0.1', 0)

    class DaemonThreadingMixIn(ThreadingMixIn):
        daemon_threads = True

    class ThreadedHTTPServer(DaemonThreadingMixIn, HTTPServer):
        """Handle requests in a separate thread."""

    # not actually going to do anything with this server, so a
    # do-nothing handler is reasonable
    httpd = ThreadedHTTPServer(server_address, BaseHTTPRequestHandler)
    server_thread = threading.Thread(target=httpd.serve_forever)
    server_thread.daemon = True
    server_thread.start()
    return httpd, server_thread 
Example #6
Source File: tcp.py    From ryu with Apache License 2.0 4 votes vote down vote up
def tcp_test_main():
    class Server(ThreadingMixIn, TCPServer):
        pass

    queue, Handler = create_length_prefixed_tcp_handler()

    server = Server(('localhost', 12345), Handler)
    server.allow_reuse_address = True

    server.serve_forever()