1、客户端和服务器聊天,可以一直聊天,直到一方发送’拜拜’。然后就可以和下一个人一直聊
server.py
import socket
def creat_server():
server = socket.socket()
server.bind(('10.7.153.127', 8081))
server.listen(512)
while True:
connect,addr = server.accept()
while True:
recv_message = connect.recv(1024)
print(recv_message.decode(encoding='utf-8'))
if recv_message.decode(encoding='utf-8')=='拜拜':
break
message = input('服务器:')
connect.send(message.encode())
if message == '拜拜':
break
connect.close()
if __name__ == '__main__':
creat_server()
client.py
import socket
def creat_client():
client = socket.socket()
client.connect(('10.7.153.127', 8081))
while True:
message = input('客户端:')
client.send(message.encode())
if message == '拜拜':
break
recv_message = client.recv(1024)
print(recv_message.decode(encoding='utf-8'))
if recv_message.decode(encoding='utf-8') == '拜拜':
break
client.close()
if __name__ == '__main__':
creat_client()
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)
image = response.content
with open('./files/03.jpg', 'wb') as f:
f.write(image)
结果: