Issue when uploading a file from client to server - python

I am working on a program that backs up files like OneDrive or iCloud.
I made everything work but when I send a file trough the sockets it just gets stuck until I close the connection.
Here is the code sample:
The client:
def UpdateFileOnServer(file):
print("[!] Uploading file to server: ",file)
with open(clientFolder+file,"rb") as f:
s.sendall(file.encode())
file = f.read()
s.sendall(file)
print("[!] File has been sent.")
The server:
with open(fileLocation,"wb") as f:
print("[!] File recieved! Downloading...")
while True:
data = conn.recv(4096)
if not data:f.close();break
f.write(data)
So to summarize again:
The client sends the file and says that it has been sent.
The server receives about 90% of the file and indefinitely hangs until I CTRL+C the client
When I do that the server finishes the file transfer and the file is successfully received.

I have found the answer to my problem thanks to all the kind commenters.
Create a server socket.
Connect with the client socket to the server socket.
Send the file with the client to the server.
When the client confirms that the file has been sent you close the connection.
The server continues to listen for connections.
Rinse and repeat!

Related

Bad gate way Error while using ftp in server

Actually we are using python3.6.8 in our server, we are trying to connect the ftp server and pushing files to the ftp server through an api call, here when we try to push the files from local it is running fine and files are being pushed but when calling api to the server it is redirecting to a 502 bad gateway error after 14.8s time when tried with postman. the server we use is AWS EC2
ftp = ftplib.FTP()
host = config.FTP_HOST
port = 21
ftp.connect(host, port)
try:
ftp.login(config.FTP_USERNAME, config.FTP_PASSWORD)
file = open(path_image, 'rb')
ftp.cwd("/DailyDump/target/")
ftp.storbinary("STOR sample_file_name" + str(yesterday_date) + ".csv", file)
file.close()
ftp.close()
except:
pass
This problem was caused due to the maximum timeout reached on the API call, hence I transferred the codes from API to stand alone code to make it run for longer time period without giving error. so there is no error in ftp logging.

simple socket programming exercise for TCP connections in Python; Error: "No connection could be made because the target machine actively refused it"

*Before you mark as duplicate please note that I am referencing this similar question found here:
Python Socket Programming - ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
unfortunately but have found anything in that post that provides a solution to my problem.
I am working on a very basic exercise designed to familiarize students with programming related to networks. This particular assignment is a common one as is described as follows:
In this assignment, you will learn the basics of socket programming for TCP connections in Python: how to create a socket, bind it to a specific address and port, as well as send and receive an HTTP packet. You will also learn some basics of HTTP header format. You can only use Python3.
You will develop a web server that handles one HTTP request at a time. Your web server should accept and parse the HTTP request, get the requested file from the server’s file system, create an HTTP response message consisting of the requested file preceded by header lines, and then send the response directly to the client. If the requested file is not present in the server, the server should send an HTTP “404 Not Found” message back to the client.
Part one specification:
Put the attached HTML file (named HelloWorld.html) in the same directory in which the server webserver.py runs. Run the server program. Determine the IP address of the host that is running the server (e.g., 128.238.251.26 or localhost). From another host, open a browser and provide the corresponding URL. For example: http://128.238.251.26:6789/HelloWorld.html. You can open a browser in the same host where the server runs and use the following http://localhost:6789/HelloWorld.html.
‘HelloWorld.html’ is the name of the file you placed in the server directory. Note also the use of the port number after the colon. You need to replace this port number with the port number that was assigned to you. In the above example, we have used port number 6789. The browser should then display the contents of HelloWorld.html. If you omit “:6789”, the browser will assume port 80 (why?), and you will get the web page from the server only if your server is listening at port 80.
Then try to get a file that is not present on the server (e.g., test.html). You should get a “404 File Not Found” message.
Part Two specification:
Write your own HTTP client to test your server. Your client will connect to the server using a TCP connection, send an HTTP request to the server, and display the server response as an output. You can assume that the HTTP request sent is a GET method. The client should take command line arguments specifying the server IP address or hostname, the port at which the server is listening, and the HTTP file name (e.g., test.html or HelloWorld.html). The following is an input command format to run the client. webclient.py <server_host> <server_port>
My code is for the Webserver is as follows:
#import socket module
from socket import *
import sys # In order to terminate the program
serverSocket = socket(AF_INET, SOCK_STREAM)
# Prepare a sever socket
# Fill in start
serverHost = '192.168.1.4'
serverPort = 56014
serverSocket.bind((serverHost, serverPort))
serverSocket.listen(5)
# Fill in end
while True:
#establish connection
print('The server is ready to receive')
connectionSocket, addr = serverSocket.accept() # Fill in start #Fill in end
try:
message = connectionSocket.recv(4096) # Fill in start #Fill in end
filename = message.split()[1]
f = open(filename[1:])
outputdata = f.readlines() # Fill in start #Fill in end
# send one http header line in to the socket
# Fill in start
connectionSocket.send("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n")
connectionSocket.send("\r\n")
# Fill in end
# Send the content of the requested file to the connection socket
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i].encode())
connectionSocket.send("\r\n".encode())
connectionSocket.close()
except IOError:
# Send HTTP response code and message for file not found
# Fill in start
connectionSocket.send("HTTP/1.1 404 Not Found\r\n")
connectionSocket.send("Content-Type: text/html\r\n")
connectionSocket.send("\r\n")
connectionSocket.send("<html><head></head><body><h1>404 Not Found</h1></body></html><\r\n>")
# Fill in end
# Close the client connection socket
# Fill in start
serverSocket.close()
# Fill in end
serverSocket.close()
sys.exit() # Terminate the program after sending the corresponding data
My code for the Webclient is as follows:
from socket import *
import sys
serverName = sys.argv[1]
serverPort = int(sys.argv[2])
fileName = sys.argv[3]
request = "GET "+str(fileName)+" HTTP/1.1"
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName, serverPort))
clientSocket.send(request.encode())
returnFromSever = clientSocket.recv(4096)
while(len(returnFromSever)>0):
print(returnFromSever.decode())
returnFromSever = clientSocket.recv(4096)
clientSocket.close()
The error I am receiving is:
"No connection could be made because the target machine actively refused it"
Admittedly, I know almost nothing about network related programming and on top of that I am not familiar with the Python syntax (my entire degree program was exclusively in Java) so I am very lost here and somewhat desperate.
If anyone could please point me in the right direction as far as how to correct this error, I would be very deeply grateful.
Thanks
The error you are getting (No connection could be made because the target machine actively refused it) means that the port you are trying to connect to is not not being listened on the server.
For example, if you try to connect to 192.168.1.1:80 (IP = 192.168.1.1, port=80) and the server on 192.168.1.1 doesn't listen on port 80, you would receive this error.
A few things I would check in your case:
Is your server IP actually 192.168.1.4 ? If not, set it to the correct IP of the interface you want to listen on. If you want to listen on all the interfaces of the server, use this: serverHost = '0.0.0.0'
Does your client code attempt to connect to the server port? The server port is 56014. You need to pass it as the second parameter of your client program (because of this line serverPort = int(sys.argv[2])).

Python TCP server throws OSError: [WinError 10057] when calling socket.shutdown(1)

I have quite a complex TCP/IP server that connects to several clients. I was running into an issue where my clients were not being updated that the socket was closing. I believe I need to call both socket.shutdown() and socket.close() to be able send a server closing advisory to all of the clients. However, I get an OS error whenever I call socket.shutdown().
OSError: [WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
Does anybody know why this is happening? Or some other way of closing the socket while still sending the advisory to all connected clients? I could always write a custom command that resets all of the clients before calling socket.close(), but something seems wrong with my server. Below is minimum reproducible example.
import socket
import selectors
class server_test:
host = ''
port = 12345
sel = selectors.DefaultSelector()
def __init__(self):
self.start_server()
self.stop_server()
#Server Functions
def start_server(self):
self.lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.lsock.bind((self.host, self.port))
self.lsock.listen()
print("Server starting, listening on ",. (self.host, self.port))
self.lsock.setblocking(False)
self.sel.register(self.lsock, selectors.EVENT_READ, data=None)
def stop_server(self):
print("Shutting server down")
self.lsock.shutdown(1)
self.lsock.close()
I believe I need to call both socket.shutdown() and socket.close() to be able send a server closing advisory to all of the clients.
No. A shutdown(1) on a connected socket will send a FIN to the peer to signal that the local system will not send more data, but it might actually accept more data. A shutdown(0) will stop receiving data locally and reject any data send by the peer. A close() essentially combines a shutdown(1) and a shutdown(0).
self.lsock.listen()
...
self.lsock.shutdown(1)
You are trying to tell the peer of self.lsock that you will no longer send data. But self.lsock is the local listener socket which is not itself connected and thus has no peer. Since you are thus try to do an operation which requires a connected socket on a socket which is not connected (only listening) it will result in the error "... socket is not connected ...".
If you want to shutdown the connection to all clients you actually have to call shutdown or close on all connected sockets (i.e. result from self.lsock.accept() and not on the listener socket.

Python EOF in FTP Transfer over PASV Connection

I'm writting a FTP Server in Python 2.7 over Sockets.
In FTP protocol, connection has to be closed by sender to define the End Of the File (EOF).
The code below is a brief example of what I am doing. Server sends file.mp4, since the server is the sender of the file, he will have to close the connection to define the end of the file.
C->S : PASV
S->Bind Port()
S->C : Entering Passive Mode (ip1,ip2,ip3,ip4,port1,port2)
C->Connect PASV (ip, port)
C->S : RETR file.mp4
S->C : Transfer file content via PASV connection....
S->Close PASV connection() = End Of File.
S->C : 226 File successfully transferred.
So far so good, but here is the error I need to handle.
If the Server has more UPLOAD speed than Client's DOWNLOAD speed (most likely).
Then the server would have already sent the file and closed connection = EOF before Client could have finished downloading it and saving it. Result, Client gets a malformatted file.
How could I figure out this?

connection refused from server unit I reset client machine

Below is the code I am running within a service. For the most part the script runs fine for days/weeks until the script hiccups and crashes. I am not so worried about the crashing part as I can resolve the cause from the error logs an patch appropriately. The issue I am facing is that sometimes when the service restarts and tries to connect to the server again, it gets a (10061, 'Connection refused') error, so that the service is unable to start up again. The bizarre part is that there is no python processes running when connections are being refused. IE no process with image name "pythonw.exe" or "pythonservice.exe." It should be noted that I am unable to connect to the server with any other machine as well until I reset computer which runs the client script. The client machine is running python 2.7 on a windows server 2003 OS. It should also be noted that the server is coded on a piece of hardware of which I do not have access to the code.
try:
EthernetConfig = ConfigParser()
EthernetConfig.read('Ethernet.conf')
HOST = EthernetConfig.get("TCP_SERVER", "HOST").strip()
PORT = EthernetConfig.getint("TCP_SERVER", "PORT")
lp = LineParser()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
reader = s.makefile("rb")
while(self.run == True):
line = reader.readline()
if line:
line = line.strip()
lp.parse(line)
except:
servicemanager.LogErrorMsg(traceback.format_exc()) # if error print it to event log
s.shutdown(2)
s.close()
os._exit(-1)
Connection refused is an error meaning that the program on the other side of the connection is not accepting your connection attempt. Most probably it hasn't noticed you crashing, and hasn't closed its connection.
What you can do is simply sleep a little while (30-60 seconds) and try again, and do this in a loop and hope the other end notices that the connection in broken so it can accept new connections again.
Turns out that Network Admin had the port closed that I was trying to connect to. It is open for one IP which belongs to the server. Problem is that the server has two network cards with two separate IP's. Issue is now resolved.

Categories