- 客户端和服务器聊天,可以一直聊天,直到一方发送’拜拜’。然后就可以和下一个人一直聊
服务器
import socket
server=socket.socket()
server.bind(('10.7.153.133',6666))
server.listen(512)
while True:
connect, addr = server.accept()
print(addr)
while True:
recv_data=connect.recv(1024)
print('客户端说:'+str(recv_data,encoding='utf-8'))
if str(recv_data,encoding='utf-8')=='拜拜':
connect.close()
break
message=input('服务器发送:')
if message=='拜拜':
connect.close()
break
connect.send(bytes(message,encoding='utf-8'))
客户端
import socket
client=socket.socket()
client.connect(('10.7.153.133',6666))
while True:
message=input('客户端发送:')
client.send(message.encode())
recv_data=client.recv(1024)
print('从服务器接收:'+str(recv_data,encoding='utf-8'))
client.close()
2,2. 下载网络图片(
https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2808438283,4249462766&fm=26&gp=0.jpg)到本地
import requests
url='https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2808438283,4249462766&fm=26&gp=0.jpg'
response=requests.get(url)
print(response)
content=response.content
with open('./files/tupian.jpg','wb') as f:
f.write(content)
tupian.jpg
网友评论