2020-01-13
def send_file_to_client(client_socket,client_addr):
""""发送文件给客户端"""
# 1. 获取文件名
file_name = client_socket.recv(1024).decode("utf-8")
# 打开文件读取数据 这里不用with 因为打开失败with不会报错
file_content = None
try:
f = open(file_name,'rb')
file_content = f.read()
f.close()
except Exception as ret:
print("没有要下载的文件%s" % file_name)
if file_content: # 如果内容不为空就发送
client_socket.send(file_content)
def main():
tcp_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
tcp_socket.bind(("10.98.29.130",8899))
tcp_socket.listen(100)
while True:
client_socket,client_addr = tcp_socket.accept()
send_file_to_client(client_socket,client_addr)
client_socket.close()
tcp_socket.close()
标签:socket,项目,python,爬虫,tcp,content,client,file,close
来源: https://www.cnblogs.com/ziju/p/12186748.html