gpt4 book ai didi

python-3.x - 如何在python中从服务器接收文件?

转载 作者:行者123 更新时间:2023-12-03 12:08:49 25 4
gpt4 key购买 nike

我已经制作了一个python脚本,您可以在其中将文件发送到远程服务器并接收它们。我可以将它们发送到服务器,但似乎无法找回它们。我已经附加了server和client.py

Server.py

import socket   # Import socket module
import os
s = socket.socket()
s.bind(('139.59.173.187', 8010)) # Binds port and IP address
s.listen(3) #wait for client to join
c, addr = s.accept()
def RecvFile():
print ("File is comming ....")

file = c.recv(1024) #recieves file name from client
print(file)

f = open(file,'wb') #open that file or create one
l = c.recv(4096) #now recieves the contents of the file
while (l):
print ("Receiving...File Data")
f.write(l) #save input to file
l = c.recv(4096) #get again until done
print("The file",file,"has been succesfully saved to the server\nConnection from:",addr)
f.close()

def SendFile():
file = c.recv(1024)
print(file)
with open(file,'rb') as f:
c.sendall(f.read())
print("File has been sent to the client:", addr)
main()


def main():
option = str(c.recv(1024), 'utf-8')
print(option)

if option[:1] == "R":
SendFile()
elif option[:2] == "S":
RecvFile()

main()
s.close()

客户端
import time
import socket
import sys
import urllib.request
#import paramiko
def login():
a = 1
while a == 1:
global user
user = input("Username:")
passw = input("Password:")
with open('User.txt') as f:
for line in f.readlines():
us, pw = line.strip().split("|", 1)
if (user == us) and (passw == pw):
print("login Successful!")
a = a+1
main()
return

print("Incorrect details, Try again!")



def register():
print("You will recieve a generated Username from your Name and age")
regName = input("First Name: ")
regAge = input("Age: ")
regPass = input("Password: ")
regUser = (regName[0:3]+regAge)
with open('User.txt', 'a') as file:

file.writelines(regUser+"|"+regPass+"\n")
print("You have Succesfully registered")
print("Your Username is:"+regUser)
print("Your Password is:"+regPass)
print("!Please keep these credentials secure!")

def main():
print("=========================Welcome to Bluetooth Projector=========================")
option = input("Here are your options:\n--> Press C to connect to the projector\n--> Press F for File System")
x = 1
while x == 1:
if option == "C" or option == "c":
optionc(x)
elif option == "F" or "f":
File_System()

def optionc():
print("Loading.....")
time.sleep(2)

print("Obtaining IP addres.....")
time.sleep(2)
print("Connecting.....")
time.sleep(3)
print("Connected")
x = x+1


def File_System():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('139.59.173.187',8010))

print("=========================Welcome to the File System =========================")
option = input("Here are your options:\n--> Press S to upload files\n--> Press R to retrieve files")
if option == "S" or option == "s":
s.send("S".encode('utf-8'))

file_name = input("Please input the file name\n>>>")
name = input("What would you like it to be saved as?\n>>>")

s.send(name.encode('utf-8'))
with open(file_name,'rb') as f:
s.sendall(f.read())
print("File has been sent to the server")
main()

elif option == "R" or option == "r":
s.send("R".encode('utf-8'))
filename = input("What is the filename you wish to recieve\n>>>")
s.send(filename.encode('utf-8'))

print ("File is comming ....")
f = open(filename,'wb') #open that file or create one
l = s.recv(1024) #now recieves the contents of the file
while (l):
print ("Receiving...File Data")
f.write(l) #save input to file
l = s.recv(1024)
print("The file",filename,"has been succesfully saved to your computer")
f.close()
while True:
check = input("Do you already have an account? Y/N \n ")

if check == "Y" or check == "y":
login()
break

elif check == "N" or check == "n":
register()
break
else:
print("pleae enter either Y or N")

请查看客户端文件中的Filesystem()。我认为这是获取文件时出现问题的第二个字节波

一个was错误是我修复的管道损坏。
但是现在实际接收的文件循环不再进行,并且停止获取文件字节。我相信这是我的错误。但是我感到困惑,因为我的代码与将文件发送到服务器没有任何复杂性几乎相同。

最佳答案

But now the actually receiving file loop doesn't go through and stops getting the file bytes. I believe this is my error.



你说的对。客户端没有迹象表明没有更多的文件内容要来,因此将永远在 recv中等待。要解决此问题,您可以让服务器在文件发送后关闭连接,或者,如果您希望保留连接,请通知客户端何时必须停止接收,例如。 G。预先发送文件长度。

关于python-3.x - 如何在python中从服务器接收文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47821583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com