zoukankan      html  css  js  c++  java
  • python多进程、多线程服务器和客户端的简单实现

    使用了多进程的服务器:

    from SocketServer import TCPServer, ForkingMixIn, ThreadingMixIn, StreamRequestHandler
    
    class Server(ThreadingMixIn, TCPServer):
        pass
    
    class Handler(StreamRequestHandler):
        def handle(self):
            addr = self.request.getpeername()
            print('Got connection from', addr)
            self.wfile.write('Thank you for connecting')
    
    server = Server(('', 1234), Handler)
    server.serve_forever()

    使用了多线程的服务器:

    from SocketServer import TCPServer, ThreadingMixIn, StreamRequestHandler
    
    class Server(ThreadingMixIn, TCPServer):
        pass
    
    class Handler(StreamRequestHandler):
        def handle(self):
            addr = self.request.getpeername()
            print 'Got connection from', addr
            self.wfile.write('Thank you for connecting')
    
    server = Server(('', 1234), Handler)
    server.serve_forever()

    客户端:

    import socket
    
    s = socket.socket()
    host = socket.gethostname()
    port = 1234
    
    s.connect((host, port))
    print(s.recv(1024))
  • 相关阅读:
    【BZOJ2287】消失之物
    【NOI2001】炮兵阵地
    【SCOI2005】互不侵犯
    【USACO2007 nov glod】玉米田
    【NOIP模拟】航班
    【NOIP模拟】闲荡
    【NOIP模拟】军队调遣
    树形地铁系统
    矩阵
    完美的集合(题解)
  • 原文地址:https://www.cnblogs.com/lakeone/p/6141249.html
Copyright © 2011-2022 走看看