- 客户端和服务器聊天,可以一直聊天,直到一方发送’拜拜’。然后就可以和下一个人一直聊
import socket
def creat_server():
server = socket.socket(family=socket.AF_INET,type=socket.SOCK_STREAM)
server.bind(("10.7.153.200",8080))
server.listen(512)
while True:
print('监听状态')
connect,addr = server.accept()
print(addr)
while True:
message = input('服务器:')
connect.send(message.encode())
recv_data = connect.recv(1024)
print(recv_data.decode())
if message == '拜拜':
connect.close()
if __name__ == '__main__':
creat_server()
创建客户端
import socket
def creat_client():
#1.创建套接字对象
client = socket.socket()
#2.连接服务器
"""
connect(服务器地址)
"""
client.connect(("[图片上传失败...(image-af22c3-1536755670067)]
10.7.153.200",8080))
while True:
#3.接收服务器发送的消息
data = client.recv(1024)
print(data.decode(encoding='utf-8'))
#4.给服务器发送消息
message = input('客户端')
client.send(message.encode())
#5.断开连接
client.close()
if __name__ == '__main__':
creat_client()
- 下载网络图片([图片上传失败...(image-689f0c-1536755571066)]
https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2808438283,4249462766&fm=26&gp=0.jpg)到本地
import requests
ur1 = 'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2808438283,4249462766&fm=26&gp=0.jpg'
response = requests.get(ur1)
with open('./files/new2.jpg', 'wb') as f:
f.write(response.content)