python如何向服务器发送文件,在Python中使用套接字向服务器发送文件

主页显示我的首页和上传应该显示的页面

在此处输入要与服务器一起存储的文件def upload():

path_name = raw_input("Enter your file directory")

open_file = open(path_name,'rb').read()

name_split = path_name.split("\\")[-1].split('.')

at = 1

s.send("SAVE-"+username+"\\"+"".join(name_split[:-1])+"."+str(at)+"."+name_split[-1]+"-")

while open_file:

current = open_file[:1024]

print current

open_file = open_file[1024:]

s.send(current)

def mainpage():

global R2

R2=Tk()

gg="white"

g="blue"

R2.geometry('720x720')

R2.title(username + " Dropbox")

R2.resizable(width=False,height=False)

logoutbt= Button(R2,text="Logout",width=10,height=2,bg=g,fg=gg,font="5",relief=RAISED,overrelief=RIDGE,command=deslogout)

upload = Button(R2,text="Upload",width=10,height=2,bg=g,fg=gg,font="5",relief=RAISED,overrelief=RIDGE,command=desupload)

retrieve = Button(R2,text="Retreive",width=10,height=2,bg=g,fg=gg,font="5",relief=RAISED,overrelief=RIDGE,command=desretreive)

logoutbt.place(x = 220,y = 500)

retrieve.place(x = 350,y = 500)

upload.place(x = 480,y = 500)

R2.mainloop()

open(path_name,'rb').close()

现在,当我添加命令mainpage()以在将文件发送到服务器后返回到主页面时,服务器陷入无限循环ServerCode

if message[0] == "SAVE":

if not os.path.exists("C:\Heights\Documents\Projects\HomeWork\Project\Server1\\Files\\"+message[1].split("\\")[0]):

os.makedirs("C:\Heights\Documents\Projects\HomeWork\Project\Server1\\Files\\"+message[1].split("\\")[0])

file =open("C:\Heights\Documents\Projects\HomeWork\Project\Server1\\Files\\"+ message[1],"wb")

content = ""

while True:

data = current_socket.recv(1024)

if not data:

break

content += data

file.write(content)

file.close()

当我不尝试返回时,文件可以很好地到达服务器,但是当我添加那一行时,服务器不会退出它接收所有文件内容的循环。另外,如果我试图在服务器写下所有数据后得到响应,那么客户端和服务器就会卡住。你知道吗

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值