Python 代码的目录不正确,如何修复

问题描述 投票:0回答:1

我在为课程编写的代码(在下载的客户端和服务器文件夹之间传输文件的服务器客户端连接)遇到一些问题。我花了大约 10 个小时进行调试,然后重新开始刷新代码。新代码将在下面。有些评论是技术问题,但主要的两个是1)如何让windows/python识别该目录(我运行它并说该目录不存在。)2)即使目录被解决,密码系统也不会也不行。我如何让它与名为的字典一起使用。(缩进是正确的,stackoverflow 希望放置缩进)

客户端代码: import socket # 导入socket模块

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                 # Reserve a port for your service.
x = True


username = input('Username: ')
password = input('Password: ')

Login = {
    "joshua": "macOS", #regardless of information used to login, still logs you in
    "scotto": "linux",
    "divetta": "windows",
    }

print("testing1") #prints but crashes too quickly

print("Successfully Logged In")
s.connect((host, port))
choice = int(input("1=Upload or 2=Download: "))
while x == True:
    if choice == 1:
        print("upload")
        upload = input("File name to upload: ")
        f = open('Client\\{upload}','rb') #ask about how to get folder to work here with filename
        print('Sending...')
        l = f.read(1024) #ask if this will make the file transfer, transfer data
        while (l):
            print('Sending...')
            s.send(l)
            l = f.read(1024)
            x = False
        f.close()
        print("Done Sending")
        s.shutdown(socket.SHUT_WR)
        print(s.recv(1024))
        s.close()
        
elif choice == 2:
    print("download")
    download = input("File name to download: ")
    f = open('Client\\{download}','rb') #ask about how to get folder to work here with filename
    print('Receiving...')
    l = f.write(1024)
    while (l):
        print('Receiving...')
        s.recv(l)
        l = f.write(1024)
        x = False
    f.close()
    print("Done Receiving")
    s.shutdown(socket.SHUT_WR)
    print(s.recv(1024))
    s.close()
        
else:
    print("invalid choice")
    x = True`

服务器代码: import socket # 导入socket模块

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                 # Reserve a port for your service.
s.bind((host, port))        # Bind to the port
s.listen(5)                 # Now wait for client connection.
c, addr = s.accept()     # Establish connection with client.
print('Got connection from', addr)

Login = {
    "joshua": "macOS",
    "scotto": "linux",
    "divetta": "windows",
    }

while True:
    l = c.recv(1024)
    while (l):
        print("Receiving...")
        f.write(l)
        l = c.recv(1024)
        f.close()
        print("Done Receiving")
        #c.close()                # Close the connection
while False:
    l = c.send(1024)
    while (l):
        print("Sending...")
        f.read(l)
        l = c.send(1024)
        f.close()
        print("Done Sending")
        #c.close()`

enter image description here

尝试一起调试和重做代码。尝试过功能,但想保持清晰,因为它对我来说有点太复杂了。

python server tcp connection client
1个回答
0
投票

要计算字符串中的表达式(如

"Client\\{upload}"
),您需要在字符串中添加f
前缀:

# here: v f = open(f"Client\\{upload}", "rb")
如果您的 Python 版本低于 3.6,请使用 

str.format

 和空占位符:

f = open("Client\\{}".format(upload), "rb")
    
© www.soinside.com 2019 - 2024. All rights reserved.