1. 客户端和服务器聊天,可以一直聊天,直到一方发送’拜拜’。然后就可以和下一个人一直聊
a.服务器:
import socket
def creat_server():
server = socket.socket()
server.bind(('10.7.153.148', 8999))
server.listen(999)
while True:
print("建立连接")
connect, addr = server.accept()
while True:
message = input("服务器:")
connect.send(message.encode())
recv_data = connect.recv(1024)
if str(recv_data, 'utf-8') == '拜拜':
print('结束对话')
break
else:
print(recv_data.decode())
connect.close()
creat_server()
"""
建立连接
服务器:hello man
hello
服务器:what are you doing ?
i writing code~~
服务器:woo~~ so cool
yeah~~ see you~
服务器:拜拜
"""
b.客户端:
import socket
def creat_client():
client = socket.socket()
client.connect(('10.7.153.148', 8999))
while True:
recv_data = client.recv(1024)
if str(recv_data,'utf-8') =='拜拜':
print('结束对话')
break
else:
print(recv_data.decode())
message = input("客户端:")
client.send(message.encode())
client.close()
creat_client()
"""
hello man
客户端:hello
what are you doing?
客户端:i writing code~~
woo~~ so cool
客户端:yeah~~ see you~~
结束对话
"""
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)
data = response.content
with open('../files/new2.png','wb')as f:
f.write(data)
样式
网友评论