py---------socketserver

同时两个客户端连接,

server 不能有input

server端根据client端的要求去执行固定的代码

 

 

server.py

#-*- coding:utf-8 -*-

import time
import socketserver

class Myserver(socketserver.BaseRequestHandler):        #必须学继承
    def handle(self):               #必须叫handle
        conn = self.request   #
        print(conn)
        time.sleep(3)
        conn.send(b'hello')
        time.sleep(4)
        conn.send(b'hello2')


myserver = socketserver.ThreadingTCPServer(('127.0.0.1',9000),Myserver)     #把类名传过来
myserver.serve_forever()        #启动socket_server 端

 

client1.py

#-*- coding:utf-8 -*-
import socket

sk = socket.socket()
sk.connect(('127.0.0.1',9000))
print(sk.recv(1024))
print(sk.recv(1024))
sk.close()

 

client2.py

import socket

sk = socket.socket()
sk.connect(('127.0.0.1',9000))
print(sk.recv(1024))
print(sk.recv(1024))
sk.close()

 

posted @ 2018-06-13 16:59  王竹笙  阅读(147)  评论(0编辑  收藏  举报