Python programming exercise - simple server and client communication


Write a simple server and use this server to communicate with clients

Based on python3.6


Code:

server:

from asyncore import dispatcher
import socket,asyncore
class ChatServer(dispatcher):
    def handle_accept(self):
        conn=self.accept()
        addr=self.accept()
        print("Connect attempt from",addr[0]) 
        
s=ChatServer()
s.create_socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('',5006))
s.listen(5)
asyncore.loop()


Client:

import socket
s=socket.socket()
host=socket.gethostname()
port=5006
s.connect((host,port))


print(s.recv(1024))



run screenshot





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325437142&siteId=291194637