write to file on a remote server in python

Feedib Com picture Feedib Com · Feb 21, 2018 · Viewed 8k times · Source

Is there any way to write to a file which is located on another server?

I have access to a powerful server with lots of resources (CPU,GPU,RAM, etc) to execute complicated python code but the amount of storage is very limited (less than 1GB). On other hand, I have an unlimited storage space on another server without computational resources.

I need a method to write the result of python code in one server, to another one. something like the following:

f = open('www.example.com/data.txt')
f.write(pythoncode.results)

I know I can read a remote file simply with liburl:

import urllib3
http = urllib3.PoolManager()
r = http.request('GET', 'www.example.com/data.txt')
r.data

So I am looking for something similar for writing instead of reading.

(I can give the remote file write permission although I know it is not a good idea in general).

Thank you in advance.

Answer

Jeff picture Jeff · Feb 22, 2018

You need to use some kind of protocol to communicate over the network and write a file. You've provided an example of using the HTTP protocol to GET a file (or resource) from the server. (HTTP also provides PUT and POST methods to create and update resources on the server but the server needs to be set up to associate the resources with files.)

A more natural alternative which exists for this purpose is the SSH File Transfer Protocol (SFTP). You can find an introduction on how to use it with Python here:

http://www.pythonforbeginners.com/modules-in-python/python-secure-ftp-module

EDIT

An alternative approach would be to do OS level file system networking to mount a remote directory into your local filesystem. Then from your scripts you can open and write to files as if they are local.

If you're using Linux you can find an introduction to NFS here:

https://www.tecmint.com/how-to-setup-nfs-server-in-linux/