4.网络编程-下载文件
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/1/14 0:35 # @Author : ChenAdong # @email : aiswell@foxmail.com import socket def download(): server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 客户端端绑定端口 server_sock.bind(("", 8081)) # 客户端连接服务器 server_sock.connect(("127.0.0.1", 8080)) data = server_sock.recv(1024) with open("test.txt", "wb") as f: f.write(data) if __name__ == "__main__": download()
上面例子是只要连接到服务器,服务器就会自动发送数据; 正常的使用场景是,先发送文件名称,然后服务器开始传送文件;