Writing into text file on web server using urllib/urllib3 - python

I am wondering if there is a way to write into .txt file on web server using urllib or urllib3. I tried using urllib3 POST but that doesnt do anything. Does any of these libraries have ability to write into files or do I have to use some other library?

It's not clear from your question, but I'm assuming that the Python code in question is not running on the web server. (Otherwise, it would be a matter of using the regular open() call.)
The answer is no, HTTP servers do not usually provide the ability to update files, and urllib does not support writing files over FTP / SCP. You will need to be either running some sort of an upload service on the server that exposes an API over HTTP (via a POST entry point or otherwise) that allows you to make requests to the server in a way that causes the file to be updated on the server's filesystem. Alternatively, you would need to use a protocol other than HTTP, such as FTP or SCP, and use an appropriate Python library for that protocol such as ftplib.

Related

Python deployment on webserver with CGI

I have a python script that I want to make accessible through a website with an userinterface.
I was experimenting with Flask, but I'm not sure this is the right tool for what I want to do.
My script takes userdata (.doc/.txt files), does something with it and returns it to the user. I don't want to save anything and I don't think that I need a database for this (is this right?). The file will be temporarily saved on the server and everything will be deleted once the user downloaded the modified file.
My webhosting provider supports Python and only accepts CGI. I read that WSGI is the preferred method to use with Python and that CGI has scaling issues and can only process one request at a time. I'm not sure if I understand this correctly. If several users would upload files at the same time, the server would only accept one request or overwrite previous requests? Or it can do one request per unique IP address/user?
Would CGI be ok for the simple get/process/return task of my python script or should I look into a hosting service that uses WSGI?
I had a look at Heroku and Render to deploy a flask app, but I think I could do that through my webhosting provider I guess.
For anyone interested in this topic,
I decided to deploy my app on render.com, which supports gunicorn (WSGI).

Script that can automatically download new data from the server to my local backup

I have an application running on linux server and I need to create a local backup of it's data.
However, new data is being added to the application after every hour and I want to sync my local backup data with server's data.
I want to write a script (shell or python) that can automatically download new added data from the linux server to my local machine backup. But I am newbie to the linux envoirnment and don't know how to write shell script to achieve this.
What is the better way of achieving this ? And what would be the script to do so ?
rsync -r fits in your use case and it's a single line command.
rsync -r source destination
or the options you need according to your specific case.
So, you don't need a python script for that, but you can still write it and let it use the command above.
Moreover, if you want the Python script to do it in an automatic way, you may check the event scheduler module.
This depends on where and how your data is stored on the Linux server, but you could write a network application which pushes the data to a client and the client saves the data on the local machine. You can use sockets for that.
If the data is available via aan http server and you know how to write RESTful APIs, you could use that as well and make a task run on your local machine every hour which calls the REST API and handles its (JSON) data. Keep in mind that you need to secure the API if the server is running online and not in the same LAN.
You could also write a small application which downloads the files every hour from the server over FTP (if you want to backup files stored on the system). You will need to know the exact path of the file(s) to do this though.
All solutions above are meant for Python programming. Using a shell script is possible, but a little more complicated. I would use Python for this kind of tasks, as you have a lot of network related libraries available (ftp, socket, http clients, simple http servers, WSGI libraries, etc.)

Overwriting variables in system libraries (smtplib.py)

So I'm using Flask for a website and I'm using the extension Flask Mail to send emails for me. Unfortunately, my email server doesn't support CRAM_MD5 (for a valid reason) so when smtplib.py (Python system library file) reaches the line that is:
preferred_auths = [AUTH_CRAM_MD5, AUTH_PLAIN, AUTH_LOGIN]
it uses CRAM, and fails because of an authentication error. On my development server, I just edited the file and switch the order of PLAIN and CRAM and faced no problems. My system admin doesn't want to make this change every time he does a server build if he can help it so I was wondering if there was a way I could overwrite that system library variable from my Flask application? My Flask application is a little large so if there is a way to do this, I can post a structure of my application. I mostly want to know if it is possible to overwrite variables in system libraries and if so, the standard procedure for doing so. Thanks!

Nodejs Server, get JSON data from Python in html client with Ajax

I need to get data (json) in my html page, with help of Ajax. I have a Nodejs server serving requests.
I have to get the json from server, which is python code to process and produce json as output.
So should i save json in db and access it? (seems complicated just for one single use)
Should i run python server, to serve the requests with json as result (call it directy from html via ajax)
Should i serve requests with nodejs alone, by calling python method from nodejs? if so how to call the python method.
If calling python requires it to run server ? which one to prefer (zeropc, or some kind of web framework?)
Which is the best solution? or Which is preferred over other in what scenario and what factors?
As I understand:
You have a server running
an app that acts as a webserver, made with nodejs.
another app in python that does not expose an HTTP api, but you would like to interact with it in some way?
You have control over the source code of both
When running big systems with parts programmed on different languages their are many ways to make it all work together.
The choice is yours, and you give us little information to help you decide which way is better. It all depends on how big are your python and nodejs apps, and what they do.
There are no magical way to call a python method from nodejs, or vice versa
Spawn persistent processes, and make them communicate with sockets, or pipes. Generally this is done with enabling libraries (like ZeroMQ + a serialization/RPC format).
Spawn persistent processes and make them communicate with a message queue in between (RabbitMQ, ActiveMQ, ...)
Spawn your nodejs webserver, and use the "child_process" module to spawn a process in python, and interact with it throught its standard input/output. There are helper libraries to do this: https://www.npmjs.com/package/python-shell
Just spawn the nodejs webserver and execute python scripts with "child_process" when you need them (no persistent python process).
Rewrite it all in python
Rewrite it all in nodejs
In your application, if you have some requirement of processing results of python server requests in your nodejs application, then you need to call the python server requests in nodejs app with request libray and then process the result. Otherwise, you should simply call python server resources through client side ajax requests.
Thanks

Is there any thing needed for https python web page

I been using python to create an web app and it has been doing well so far. Now I would like to encrypt the transmission of the data between client and server using https. The communication is generally just post form and web pages, no money transactions are involve. Is there anything I need to change to the python code except setting the server up with certificate and configurate it to use https? I see a lot of information regarding ssl for python and I not sure if I need those modules and python setup to make https work.
Thanks
Typically, the ssl part for Python web app is managed by some frontend web server like nginx, apache or so.
This does not require any modification of your code (assuming, you are not expecting user to authenticate by ssl certificate on client side, what is quite exotic, but possible scenario).
If you want to run pure Python solution, I would recommend using cherrypy, which is able providing rather reliable and performant web server part (it will be very likely slower then served behind nginx or apache).

Categories