客户端
import socket
def main():
tcp_client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = input('请输入下载服务器的ip:')
port = int(input('请输入下载服务器的端口:'))
tcp_client_socket.connect((ip, port))
print("已连接至" + ip)
while True:
command = input('请输入命令:')
tcp_client_socket.send(command.encode('utf-8'))
recv_data = {}
exec("recv_data = " + tcp_client_socket.recv(8388608).decode("utf-8"), recv_data)
recv_data = recv_data["recv_data"]
if recv_data["type"] == "file":
with open(recv_data["name"], "wb") as file:
file.write(recv_data["content"])
elif recv_data["type"] == "code":
if recv_data["name"] == "ok" and recv_data["content"] == "exit":
break
elif recv_data["name"] == 404:
print(recv_data["content"] + "不存在!")
elif recv_data["name"] == -1:
print("命令" + recv_data["content"] + "错误!")
elif recv_data["type"] == "list":
print(recv_data["name"] + "下的文件及目录:")
for i in recv_data["content"]:
print(i)
tcp_client_socket.close()
if __name__ == '__main__':
main()
服务端
import os
import socket
def send_client(new_client_socket, client_add):
global run
command = new_client_socket.recv(4096).decode('utf-8')
file_content = {"type": "", "name": "", "content": ""}
if command == "exit":
file_content["type"] = "code"
file_content["name"] = "ok"
file_content["content"] = "exit"
elif command.split(" ")[0] == "download" and len(command.split(" ")) > 1:
if os.path.isfile(command.split(" ")[1]):
file_content["type"] = "file"
file_content["name"] = command.split(" ")[1].split("\\")[-1].split("/")[-1]
with open(command.split(" ")[1], "rb") as file:
file_content["content"] = file.read()
print("用户下载了{}!".format(file_content["name"]))
else:
file_content["type"] = "code"
file_content["name"] = 404
file_content["content"] = command.split(" ")[1].split("\\")[-1].split("/")[-1]
print("用户试图下载{}!".format(file_content["name"]))
elif command.split(" ")[0] == "dir" and len(command.split(" ")) > 1:
if os.path.isdir(command.split(" ")[1]):
file_content["type"] = "list"
file_content["name"] = command.split(" ")[1]
file_content["content"] = os.listdir(file_content["name"])
print("用户访问了{}!".format(file_content["name"]))
else:
file_content["type"] = "code"
file_content["name"] = 404
file_content["content"] = command.split(" ")[1]
print("用户试图访问{}!".format(file_content["name"]))
elif command.split(" ")[0] == "dir":
file_content["type"] = "list"
file_content["name"] = os.getcwd()
file_content["content"] = os.listdir(file_content["name"])
print("用户访问了{}!".format(file_content["name"]))
else:
file_content["type"] = "code"
file_content["name"] = -1
file_content["content"] = command
new_client_socket.send(str(file_content).encode("utf-8"))
if command == "exit":
exit()
def main():
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_server_socket.bind(('', int(input('请输入这个服务器端口:'))))
tcp_server_socket.listen(128)
while True:
new_client_socket, client_add = tcp_server_socket.accept()
print("{}已连接,端口:{}".format(*client_add))
send_client(new_client_socket, client_add)
new_client_socket.close()
if __name__ == '__main__':
main()
分别运行后,客户端输入download server.py会出现以下情况:
Traceback (most recent call last):
File "C:/Users/123/Desktop/文件/python/socket文件下载/客户端/client.py", line 32, in <module>
main()
File "C:/Users/123/Desktop/文件/python/socket文件下载/客户端/client.py", line 13, in main
exec("recv_data = " + tcp_client_socket.recv(8388608).decode("utf-8"), recv_data)
ConnectionAbortedError: [WinError 10053] 你的主机中的软件中止了一个已建立的连接。
服务端给权限没有,现在是服务端拒绝链接,还有服务端口开放没?