I need to upload a file to a remote location using python – wsgi.
Remote location means not the server which host the python application, it is a different server.
I tried upload a file to the server which exactly the application host. And it is succeeded.
using following code,
post = cgi.FieldStorage(fp=environ['wsgi.input'],environ=environ,keep_blank_values=True)
self.msg = str(post)
try:
fileitem = post['Filedata']
if fileitem.file:
filename = fileitem.filename.decode('utf8').replace('\\','/').split('/')[-1].strip()
if not filename:
raise Exception('No valid filename specified')
file_path = os.path.join(self.uploadpath, filename)
# Using with makes Python automatically close the file for you
counter = 0
with open(file_path, 'wb') as output_file:
while 1:
data = fileitem.file.read(1024)
# End of file
if not data:
break
output_file.write(data)
counter += 1
if counter == 100:
counter = 0
self.msg = "Uploaded Successfully !!!! "
except:
pass
But this is for the the local location which the python application hosted.
I search about Google how to upload a file to remote location in python.
But I can’t find a proper solution for that. Some are suggested to do scp from python code.
more..
Let say there are two servers called A and B. My python-wsgi application for file uploading is hosted on server A. In the python-wsgi application there is a web form to select the file by user when user click upload then that file need to uploaded to server B (known location)
Any one knows a way or library to upload a file to remote location using python?
Thanks in advance.
For HTTP, you won’t have any chance to put a file on the server without an adequate counterpart on the server-side, i.e., a Python / Perl / PHP / Ruby / whatever script that takes the data you provide in a HTTP POST – request and writes them to a file. I’d recommend setting up an FTP-server. That’s exactly what FTP was invented for.
Usually, most HTTP servers have FTP as well. How do you put your HTML-files on the server? FTP is kind-of minimal access needed to do so. It’s really not hard to set up.