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()`
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.
There are a number of issues here:
First off, you need to use f-strings if you want to use a variable in a string.
If you check the error message, it tells you that it's trying to find a file literally called Client\\{upload}
. Obviously, such a file doesn't exist. What you actually want is for the value of upload
to be inserted into the string i.e. if upload
is some_file.txt
, it should try to open the file Client\\some_file.txt
.
To achieve this, you can use f-strings: f"Client\\{upload}"
. Notice the f
before the starting quotes, it's important and causes Python to replace {upload}
with the value of the upload
variable.
Second, in the download case, you are opening the file for reading (open(..., 'rb')
, the r
stands for read
) but you are writing to the file and hence need to open it for writing using open(..., 'wb')
.
Thirdly, the elif choice == 2
case is indented incorrectly, on a different level than the matching if
above it.
And your server code seems completely broken in general, I can't even really tell how it's supposed to work. You're doing stuff on a file f
that was never declared, you have a while False
loop which makes no sense (that means, "never run this code"), you are receiving from and sending to the same socket. Also, what's the point of the while True
loop anyways?
I'll also point out some style issues:
while (l)
-> while l
Instead of f = open(..)
and then later f.close()
, you should pretty much always use:
with open(..) as f:
...
f.read() # or f.write()
...
Python will automatically close the file at the end of the with
block, even if an exception is thrown inside it, which isn't the case if you manually call f.close()
.
x == True
is just the same thing as x
if x
can only be True
or False
.
x
is a terrible name for a variable that isn't an x-coordinate or has a very tiny scope, it doesn't tell me anything about what it actually means. The same applies to l
, actually, you should never use l
as a variable name ever since it can be indistinguishable from 1
or I
in some fonts. s
and f
are reasonably fine since they are easily recognizable as "socket" and "file" from how they are initialized and are commonly used that way.