Concurrent chat on 035server and concurrent on 035server

Source: Internet
Author: User

Concurrent chat on 035server and concurrent on 035server

1 import socketserver 2 3 class MyServer (socketserver. baseRequestHandler): 4 def handle (self): # The process of connecting each client to execute 5 print ('server start') 6 while True: 7 conn = self. request 8 print (self. client_address) 9 while True: 10 client_data = conn. recv (1024) 11 print (str (client_data, 'utf8') 12 print ('waiting... ') 13 conn. sendall (client_data) 14 conn. close () 15 16 if _ name _ = '_ main _': 17 server = socketserver. threadingTCPServer ('2017. 0.0.1 ', 8888), MyServer) 18 server. serve_forever () # activate this Server Object
Server. py
 1 import  socket 2  3 def connect_server(): 4     sk = socket.socket() 5     server_address = ('127.0.0.1',8888) 6     sk.connect(server_address) 7     return sk 8  9 if __name__ == '__main__':10     sk = connect_server()11     while True:12         inp = input('>>>')13         if inp == 'exit':14             break15         sk.send(bytes(inp,'utf8'))16         print('waiting...')17         data = sk.recv(1024)18         print(str(data,'utf8'))19     sk.close()
Client. py

Concurrent chat:
1. inherit socketserver. BaseRequestHandler
2. Use socketserver. ThreadingTCPServer ('192. 0.0.1 ', 127), MyServer) to get a server object.

To send information to each other, you can change it to this in the handle method. There are still many shortcomings in chatting here.

 

Adjust the execution process and check the python implementation source code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.