python socket

来源:互联网 发布:关系图算法 编辑:程序博客网 时间:2024/06/14 05:13

1.处理客户端套接字比处理服务器套接字容易,因为服务器必须准备随时处理客户端的连接,同时还要处理多个连接,而客户机只是简单的连接,完成事务,断开连接。

服务器端的代码

import sockets=socket.socket()host = socket.gethostname()print(host)port=1234s.bind((host,port))s.listen(5)while True:    c,addr=s.accept()    print("Got connection from",addr)    c.send(b"Thank you for your connection")    c.close

客户端的代码:

import sockets=socket.socket()host = socket.gethostname()port = 1234s.connect((host,port))print(s.recv(1024))

server运行结果:

KPF-PCGot connection from ('218.197.219.82', 25533)

client运行结果:

b'Thank you for your connection'Process finished with exit code 0



0 0