python socket 小例子
python#serverimportsockethost=''port=21567addr=(host,port)sercli=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #TCP连接sercli.bind(addr)sercli.listen(5)while True:print('等待连接...')cli
·
python
#server
import socket
host=''
port=21567
addr=(host,port)
sercli=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #TCP连接
sercli.bind(addr)
sercli.listen(5)
while True:
print('等待连接...')
clicli,addr=sercli.accept()
print('{}连接'.format(addr))
while True:
info=clicli.recv(1024)
if not info:
break
print('来自于客户说:{}'.format(info))
re=input('你说:') #python2.7用raw_input
clicli.send(re.encode('utf-8'))
clicli.close()
sercli.close()
#client
import socket
host='192.168.132.1' #服务端IP
port=21567
addr=(host,port)
clicli=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
clicli.connect(addr)
while True:
info=input('你说:')
clicli.send(info.encode('utf-8'))
re=clicli.recv(1024)
if not re:
break
print('主机说:{}'.format(re))
clicli.close()
更多推荐
已为社区贡献1条内容
所有评论(0)