1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| import socket
def send_file(client_socket, client_addr): file_content = None recv_data = client_socket.recv(1024).decode("gbk") print("%s 需要下载 %s" % (str(client_addr), recv_data)) try: f = open(recv_data, "rb") file_content = f.read() f.close() except Exception as res: print("没有需要下载的文件(%s)" % recv_data) if file_content: client_socket.send(file_content)
def main(): server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind(("", 8080)) server_socket.listen(128) while True: client_socket, client_addr = server_socket.accept() send_file(client_socket, client_addr) client_socket.close() server_socket.close()
if __name__ == "__main__": main()
|