Ejemplo n.º 1
0
def tcprpcserver(host,port, max_children, timeout, poll_interval, forking, daemon_threads, dispatcher):
	if forking:
		tcprpcserver = ForkingTCPServer(server_address = (host,port), RequestHandlerClass = RPCStreamRequestHandler)
		mode = _('Forking')
	else:
		tcprpcserver = ThreadingTCPServer(server_address = (host,port), RequestHandlerClass = RPCStreamRequestHandler)
		tcprpcserver.daemon_threads = daemon_threads
		mode = _('Threading')
	tcprpcserver.serializer =  Packer()
	tcprpcserver._dispatcher = dispatcher
	tcprpcserver.max_children = max_children
	tcprpcserver.timeout = timeout
	_logger = logging.getLogger("listener.tcprpcserver")
	_logger.info(_("service tcprpc running on %s port %s in mode %s") % (host, port, mode))
	tcprpcserver.serve_forever(poll_interval = poll_interval)
Ejemplo n.º 2
0
 def run(self):
     serv = ForkingTCPServer(('', self.port), self.handle)
     self.log.info('start server on port {}'.format(self.port))
     serv.serve_forever()
Ejemplo n.º 3
0
from socketserver import ForkingTCPServer, StreamRequestHandler, ForkingMixIn
from controllers import *

class MessageHandler(StreamRequestHandler):

  def handle(self):
    self.writeString(
      "GreyH@t Stepping into Security - Lab Server\n"
      + "v1.3\n"
      + "------------------\n"
      + "Available commands: [l[ogin], r[egister], i[nfo], h[elp], c[lear], q[uit]]\n"
      )
    while True:
      last_command = mainmenu.MainMenu().handle(self)
      if last_command == "quit" or last_command == 'q':
        return
      if last_command == "logout":
        self.writeString("Ok. You are now logged out.\n")

  def writeString(self, string):
    self.wfile.write(bytes(string, "utf-8"))

if __name__ == '__main__':
  server = ForkingTCPServer(("0.0.0.0", 1000), RequestHandlerClass=MessageHandler, bind_and_activate=False)
  server.allow_reuse_address = True
  server.server_bind()
  server.server_activate()
  print("Now listening on port 1000!")
  server.serve_forever()
Ejemplo n.º 4
0
                        if sock.sendall(data) == 0:
                            return
                        data = None
                    if writable_sock == self.wfile and received is not None:
                        # print("wfile writeable, sending " + str(len(received)))
                        if self.wfile.write(received) == 0:
                            return
                        received = None
                for readable_sock in readable:
                    if readable_sock == self.rfile and data is None:
                        data = self.rfile.read(1024)
                        # print("rfile readable, read " + str(len(data)))
                        if len(data) == 0:
                            return
                    if readable_sock == sock and received is None:
                        received = sock.recv(1024)
                        # print("sock readable, read " + str(len(received)))
                        if len(received) == 0:
                            return

        finally:
            print("closing socket" + str(sock))
            sock.close()

if __name__ == '__main__':
    # Create server and bind to set ip
    myserver = ForkingTCPServer((BIND_HOST, BIND_PORT), SockHandler)

    # activate the server until it is interrupted with ctrl+c
    myserver.serve_forever()
Ejemplo n.º 5
0
#!/usr/bin/python3

from socketserver import BaseRequestHandler, TCPServer
from socketserver import ForkingTCPServer, ThreadingTCPServer

class EchoHandler(BaseRequestHandler):
    def handle(self):
        print("got connection from", self.client_address)
        while True:
            data = self.request.recv(4096)
            print('receive', data.decode())
            if data:
                sent = self.request.send(data)
            else:
                print("disconnect", self.client_address)
                self.request.close()
                break



if __name__ == "__main__":
    listen_address = ("0.0.0.0", 9981)
    #server =TCPServer(listen_address, EchoHandler)
    server = ForkingTCPServer(listen_address, EchoHandler)
    server.serve_forever()
Ejemplo n.º 6
0
            fcontent = open(storagedir + x + '.content', 'rb')

        self.send_response(200)
        headerstr = fhead.read()
        headermap = eval(headerstr)
        for key, value in headermap.items():
            if key != 'Date':
                self.send_header(key, value)
        self.end_headers()
        while True:
            chunk = fcontent.read(chunksize)
            if chunk:
                self.wfile.write(hex(len(chunk))[2:].encode('utf-8'))
                self.wfile.write('\r\n'.encode('utf-8'))
                self.wfile.write(chunk)
                self.wfile.write('\r\n'.encode('utf-8'))
            else:
                self.wfile.write(hex(0)[2:].encode('utf-8'))
                self.wfile.write('\r\n'.encode('utf-8'))
                self.wfile.write('\r\n'.encode('utf-8'))
                break
        fcontent.close()
        fhead.close()

if not os.path.exists(storagedir):
    os.makedirs(storagedir)
httpd = ForkingTCPServer(('', port), Proxy)
print('start my proxy at', port)
httpd.serve_forever()

Ejemplo n.º 7
0
def test_threading_tcp_server():
    # 同时处理多个客户端请求,默认无上限, 很容易遭受饱和攻击
    # serv = ThreadingTCPServer(('',20000),EchoHandler)
    serv = ForkingTCPServer(('', 20000), EchoHandler)
    serv.serve_forever()