- 客户端和服务器聊天,可以一直聊天,直到一方发送’拜拜’。然后就可以和下一个人一直聊
服务器:
import socket
server = socket.socket()
server.bind(('10.7.153.120', 1234))
server.listen(512)
while True:
while True:
connect,addr = server.accept()
print('正在监听')
print(addr)
data = connect.recv(512)
if data.decode() == '拜拜':
break
print(data.decode())
msg = input('服务器:')
if msg == '拜拜':
break
connect.send(msg.encode())
server.close()
客户端:
import socket
client = socket.socket()
client.connect(('10.7.153.120', 1234))
while True:
msg = input('客户端:')
client.send(msg.encode())
# if msg == '拜拜':
# break
data = client.recv(512)
print(data.decode())
client.close()
- 下载网络图片(https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2808438283,4249462766&fm=26&gp=0.jpg)到本地
url = 'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2808438283,4249462766&fm=26&gp=0.jpg'
response = requests.get(url)
with open('./files/load.jpg','wb') as f:
f.write(response.content)