pythonservertcpconnectionclient

Python code has incorrect directory, how do I fix


I'm having some issues with a code I'm writing for class (a server client connection that transfers files to and from a client and server folder in my downloads). I've spent about 10 hours debugging and started over with a refresh of the code all together. The new code will be down below. some comments are technically questions, but the main two are 1) how do you make windows/python recognize the directory (I run it and says the directory doesn't exist.) 2) even if the directory was solved, the password system doesn't work either. How would I get it to work with the dictionary named.(Indents are correct, stackoverflow wanted indents placed)

Client Code: import socket # Import socket module

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`

Server Code: import socket # Import socket module

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

Tried debugging and redoing the code all together. Tried functions but would like to stay clear as it is a little too complicated for me.


Solution

  • There are a number of issues here:

    I'll also point out some style issues: