您的位置:首页 > 理论基础 > 计算机网络

python 3 TCP简单小程序

2015-11-30 17:49 751 查看
ChatServer.py

import socket

sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print (sock)
sock.bind(('127.0.01',8000))
sock.listen(2)
clientsocket,address=sock.accept();
print('connection from ',address);
while 1:
data=clientsocket.recv(1024)
if not data :break
print('received from client',repr(data))
print('echo',repr(data))
clientsocket.send(data)
clientsocket.close()
sock.close()

ChatClient.py
import socket

clientsocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
clientsocket.connect(('127.0.0.1',8000))
while 1:
data=input("请输出要传达的消息")
clientsocket.send(data.encode())
if not data:break
newdata=clientsocket.recv(1024)
print('received from server:',repr(newdata))
clientsocket.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: