I’m trying to get some code to work that I found on how to transfer files in a LAN environment. The code is as follows:
#*********************************************
#*
#*
#* Server Code
#*
#*
#**********************************************
import socket
import tqdm
import os
# device's IP address
SERVER_HOST = '0.0.0.0'
SERVER_PORT = 5001
BUFFER_SIZE = 4096
SEPARATOR = "<SEPARATOR>"
# create the server socket
# TCP socket
s = socket.socket()
# bind the socket to our local address
s.bind((SERVER_HOST, SERVER_PORT))
# enabling our server to accept connections
s.listen(5)
print(f"[*] Listening as {SERVER_HOST}:{SERVER_PORT}")
# accept connection if there is any
client_socket, address = s.accept()
# if below code is executed, that means the sender is connected
print(f"[+] {address} is connected.")
# receive the file infos
# receive using client socket, not server socket
received = client_socket.recv(BUFFER_SIZE).decode()
filename, filesize = received.split(SEPARATOR)
# remove absolute path if there is
filename = os.path.basename(filename)
# convert to integer
filesize = int(filesize)
# now, receive the file
progress = tqdm.tqdm(range(filesize), f"Receiving {filename}", unit="B", unit_scale=True, unit_divisor=1024)
with open(filename, "wb") as f:
while True:
#read 1024 bytes from the socket (receive)
bytes_read = client_socket.recv(BUFFER_SIZE)
if not bytes_read:
# nothing is received
# file transmitting is done
break
#write to the file the bytes we just received
f.write(bytes_read)
# update the progress bar
progress.update(len(bytes_read))
# close the client socket
client_socket.close()
# close the server socket
s.close()
But I get this error:
File “C:\python311\Server Code.py” , line 28, in
client_socket, address = s.accept()
^^^^^^^^^^^^
File “C:\python311\lib\socket.py”, line 294, in accept
fd, addr = self._accept()
^^^^^^^^^^^^^^^^
oserror: [winerror10022] An invalid argument was supplied
Any help would be appreciated.
Mike