- 客户端和服务器聊天,可以一直聊天,直到一方发送’拜拜’。然后就可以和下一个人一直聊
import socket
def creat_server():
server = socket.socket()
server.bind(('10.7.153.122',8080))
server.listen(512)
while True:
connect,addr = server.accept()
while True:
message = input('服务器:')
connect.send(message.encode())
if message == '拜拜':
break
recv_data = connect.recv(1024)
print(str(recv_data, 'utf-8'))
if recv_data == '拜拜':
break
connect.close()
creat_server()
import socket
def creat_client():
client = socket.socket()
client.connect(('10.7.153.122',8080))
while True:
data = client.recv(1024)
receive_data = data.decode(encoding='utf-8')
print(receive_data)
if receive_data == '拜拜':
break
message = input("客户端:")
client.send(message.encode())
if message == '拜拜':
break
client.close()
- 下载网络图片[图片上传失败...image-3b53d8-1536749816482)]
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)
picture =response.content
with open('./files/download.jpg','wb') as f:
f.write(picture)