import socket
import time
def header():
"""describe what this function does
#you will see a hello page and with information about how to
use this system
#you can input the number you see to select what you want to do"""
print("*" * 50)
print("欢迎使用文件传输系统".center(40))
print("请选择您要进行的操作代码:\n1.服务器下载\n2.服务器上传\n3.退出系统")
def download_file(tcpOrder_socket):
"""describe what this function does
#this is a function that you can input your order to the server
and then you will download the file that you want to your PC
if the server does have the file that you want,it will tell you this information
also, you can download times before you quit this """
tcpOrder_socket.send(choice_num.encode("utf-8")) # 给服务器下命令
while True:
# 睡眠1s 防止频繁请求带来异常
time.sleep(1)
# 获取要发送的信息
file_name = input("请输入你要查找的文件名:")
# 准备发送内容至服务器
# time.sleep(1)
tcpOrder_socket.send(file_name.encode("utf-8"))
# 接收服务器反馈信息并限制字节数为1024
recv_msg = tcpOrder_socket.recv(1024)
# 此处需要标识唯一的字符去判断服务器有没有找到目标文件
if recv_msg.decode("utf-8") == "No such file or directory":
print("文件不存在")
continue
else: # 如果收到了数据
print(recv_msg.decode("utf-8"))
print("downloading。。。。")
# 更换电脑后需要更改地址
with open(r"/home/itcast/Desktop/summer/%s" % file_name, "wb") as dowanload_file:
dowanload_file.write(recv_msg) # 覆盖写入
print("download complete!")
ask_again = input("是否继续下载?1/继续 0/退出")
if ask_again == "1":
tcpOrder_socket.send(ask_again.encode("utf-8"))
continue
elif ask_again == "0":
# 向服务器发送本次下载停止命令
tcpOrder_socket.send(ask_again.encode("utf-8"))
tcpOrder_socket.close()
break
def update_file(tcpOrder_socket):
"""describe what this function does
#this is a function that you can update your file to the server
and the server will save the file at the right path
also you can update times before you quit"""
tcpOrder_socket.send(choice_num.encode("utf-8"))
while True:
updateFile_name = input("请输入你要上传的文件名")
# tcpOrder_socket.send(updateFile_name.encode("utf-8"))
# time.sleep(1)
print("本地搜索中")
# 捕捉异常 打开目标文件
try:
file_content = ""
# 更换电脑后需要更改地址
souce_file = open(r"/home/itcast/Desktop/summer/%s" % updateFile_name, "r")
tcpOrder_socket.send(updateFile_name.encode("utf-8"))
time.sleep(1)
file_content = souce_file.read()
except Exception as fault_info:
# print(fault_info)
print("文件不存在")
# 此处需要给服务器发送文件不存在命令
# tcpOrder_socket.send("No such file or directory".encode("utf-8"))
continue
if not updateFile_name:
continue
# tcpOrder_socket.close()
elif file_content:
# 将内容发送给客户端
tcpOrder_socket.send(file_content.encode("utf-8"))
print("上传完成!")
# 再次接收客户端命令
ask_again = input("是否继续上传?1/继续 0/退出")
if ask_again == "0":
# 向服务器发送本次下载停止命令
tcpOrder_socket.send(ask_again.encode("utf-8"))
tcpOrder_socket.close()
break
else:
# 即使命令出错也当作再次进行上传任务
tcpOrder_socket.send("1".encode("utf-8"))
continue
server_ip = ""
server_port = ""
while True:
server_ip = input("请输入服务器IP:")
server_port = input("请输入服务器Port:")
while True:
header()
# 创建套接字对象
try: # 抓取OSError: [Errno 99] Cannot assign requested address 异常
tcpOrder_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 强制允许端口复用
tcpOrder_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
locall_addr = ("", 7890)
# 绑定本地信息
tcpOrder_socket.bind(locall_addr)
# print(tcpOrder_socket)
tcpOrder_socket.connect((server_ip, int(server_port)))
choice_num = input("请输入操作代码:")
except Exception as link_fault:
continue
# 下载模式
if choice_num == "1":
# 调用下载函数
download_file(tcpOrder_socket)
# 上传模式
elif choice_num == "2":
# 调用上传函数
update_file(tcpOrder_socket)
elif choice_num == "3":
print("system quit!")
tcpOrder_socket.close()
break
else:
print("请输入正确操作代码")
python_example_08(文件上传与下载(客户端))
猜你喜欢
转载自blog.csdn.net/weixin_44786482/article/details/89530507
今日推荐
周排行