- 客户端和服务器聊天,可以一直聊天,直到一方发送’拜拜’。然后就可以和下一个人一直聊。
服务器.py代码如下:
import socket
server = socket.socket()
server.bind(('10.7.153.115',8080))
server.listen(5)
print('监听中')
while True:
conn, addr = server.accept()
while True:
message = input('服务器:')
conn.send(message.encode())
data = conn.recv(1024).decode('utf-8')
print(data)
if data == '拜拜' or message == '拜拜':
print('本次通话结束,等待下位用户接入')
break
conn.close()
客户端.py代码如下:
import socket
client = socket.socket()
client.connect(('10.7.153.115',8080))
print('连接中')
while True:
data = client.recv(1024).decode('utf-8')
print(data)
if data == '拜拜':
print('\n通话结束')
break
message1 = input('客户端:')
client.send(message1.encode())
if message1 == '拜拜':
print('\n通话结束')
break
client.close()
- 下载网络图片(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)
with open('./new.jpg','wb') as f:
f.write(response.content)