无法上载不同的目录';在Python中使用套接字将文件发送到服务器
我正在尝试从我的客户端程序将文件上载到服务器。我可以成功上载来自同一目录的文件。但每当我从不同的目录上传文件时,它都无法识别该文件,并且显示文件不存在。有人能指出我在这里做错了什么吗?提前谢谢 图像(当我从桌面上传文件时): 我的客户代码:无法上载不同的目录';在Python中使用套接字将文件发送到服务器,python,sockets,Python,Sockets,我正在尝试从我的客户端程序将文件上载到服务器。我可以成功上载来自同一目录的文件。但每当我从不同的目录上传文件时,它都无法识别该文件,并且显示文件不存在。有人能指出我在这里做错了什么吗?提前谢谢 图像(当我从桌面上传文件时): 我的客户代码: def uploadfiletoserver(): try: # open file selector directory = filedialog.askopenfilename(parent=root, init
def uploadfiletoserver():
try:
# open file selector
directory = filedialog.askopenfilename(parent=root, initialdir="/", title='Please select a directory')
filename = (os.path.basename(directory))
if os.path.isfile(filename):
s.send(str("fup~" + filename).encode("utf-8"))
s.send(str.encode("EXISTS " + str(os.path.getsize(filename))))
filesize = int(os.path.getsize(filename))
userResponse = s.recv(1024).decode("utf-8")
if userResponse[:2] == 'OK':
with open(filename, 'rb') as f:
bytesToSend = f.read(1024)
s.send(bytesToSend)
totalSend = len(bytesToSend)
while int(totalSend) < int(filesize):
bytesToSend = f.read(1024)
totalSend += len(bytesToSend)
s.send(bytesToSend)
sys.stdout.write("\r|" + "█" * int((totalSend / float(filesize)) * 50) + "|{0:.2f}".format(
(totalSend / float(filesize)) * 100) + "% ")
sys.stdout.flush()
print("\nUpload Completed!")
else:
print("File Does Not Exist!")
except:
print("Error")
# send file
def fup(filename, conn):
if os.path.isfile(filename):
conn.send(str.encode("EXISTS " + str(os.path.getsize(filename))))
filesize = int(os.path.getsize(filename))
userResponse = conn.recv(1024).decode("utf-8")
if userResponse[:2] == 'OK':
with open(filename, 'rb') as f:
bytesToSend = f.read(1024)
conn.send(bytesToSend)
totalSend = len(bytesToSend)
while int(totalSend) < int(filesize):
bytesToSend = f.read(1024)
totalSend += len(bytesToSend)
conn.send(bytesToSend)
else:
conn.send("ERROR".encode("utf-8"))
def uploadfiletoserver():
尝试:
#打开文件选择器
directory=filedialog.askopenfilename(parent=root,initialdir=“/”,title='Please select a directory')
filename=(os.path.basename(目录))
如果os.path.isfile(文件名):
s、 发送(str(“fup~”+文件名).encode(“utf-8”))
s、 发送(str.encode(“EXISTS”+str(os.path.getsize(filename)))
filesize=int(os.path.getsize(文件名))
userResponse=s.recv(1024)。解码(“utf-8”)
如果userResponse[:2]=“确定”:
将open(filename,'rb')作为f:
bytesToSend=f.read(1024)
s、 发送(bytesToSend)
totalSend=len(bytesToSend)
当int(totalSend)
我的服务器代码:
def uploadfiletoserver():
try:
# open file selector
directory = filedialog.askopenfilename(parent=root, initialdir="/", title='Please select a directory')
filename = (os.path.basename(directory))
if os.path.isfile(filename):
s.send(str("fup~" + filename).encode("utf-8"))
s.send(str.encode("EXISTS " + str(os.path.getsize(filename))))
filesize = int(os.path.getsize(filename))
userResponse = s.recv(1024).decode("utf-8")
if userResponse[:2] == 'OK':
with open(filename, 'rb') as f:
bytesToSend = f.read(1024)
s.send(bytesToSend)
totalSend = len(bytesToSend)
while int(totalSend) < int(filesize):
bytesToSend = f.read(1024)
totalSend += len(bytesToSend)
s.send(bytesToSend)
sys.stdout.write("\r|" + "█" * int((totalSend / float(filesize)) * 50) + "|{0:.2f}".format(
(totalSend / float(filesize)) * 100) + "% ")
sys.stdout.flush()
print("\nUpload Completed!")
else:
print("File Does Not Exist!")
except:
print("Error")
# send file
def fup(filename, conn):
if os.path.isfile(filename):
conn.send(str.encode("EXISTS " + str(os.path.getsize(filename))))
filesize = int(os.path.getsize(filename))
userResponse = conn.recv(1024).decode("utf-8")
if userResponse[:2] == 'OK':
with open(filename, 'rb') as f:
bytesToSend = f.read(1024)
conn.send(bytesToSend)
totalSend = len(bytesToSend)
while int(totalSend) < int(filesize):
bytesToSend = f.read(1024)
totalSend += len(bytesToSend)
conn.send(bytesToSend)
else:
conn.send("ERROR".encode("utf-8"))
#发送文件
def fup(文件名,conn):
如果os.path.isfile(文件名):
conn.send(str.encode(“EXISTS”+str(os.path.getsize(filename)))
filesize=int(os.path.getsize(文件名))
userResponse=conn.recv(1024)。解码(“utf-8”)
如果userResponse[:2]=“确定”:
将open(filename,'rb')作为f:
bytesToSend=f.read(1024)
conn.send(bytesToSend)
totalSend=len(bytesToSend)
当int(totalSend)
您正在使用filename=(os.path.basename(directory))剥离路径;这只返回文件名。因此,os.path.isfile(filename)
将失败,除非文件位于当前目录中
你可能只是想要这个:
filename=filedialog.askopenfilename(parent=root, initialdir="/", title='Please select a directory')
我尝试了这个,但不幸的是它返回了我arr=pickle.loads(s.recv(1024))\u pickle.UnpicklingError:无效的加载键“E”。
它说上载完成,但我的服务器没有收到任何files@JacksonLim因此,这是您要解决的下一个问题。您最初错误的原因是您使用的是basename()
。