Python Forum
how to run a Python programm on a web server?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to run a Python programm on a web server?
#1
I have wrote a parcer and want to try it from a webserver.
I am paywing for a hosting at goneo.de. The hosting has a webserwer with Python and MySQL.

[Image: goneo.png]

In the manual the service provider wtites the folowing:

Python - Basics and Information

Prerequisites:

Scripts must be in the directory /cgi-bin and have the file rights 750.
Scripts must output a standard header as first line, e.g. Content-type: text/html
Scripts must be in Unix file format and uploaded in ASCII mode.
If these settings are not met, only "Internal Server Error" will appear and the scripts will not be executed.

You can change the file and directory permissions with your FTP program using the "chmod" command.

For information on how this works with your FTP program, please refer to the corresponding
documentation of your software. Please understand that we cannot offer support for this.

Interpreter path for Python:
#!/usr/local/bin/python

Example script "Hello World!" in Python:
(filename e.g. hallowelt.py)

#!/usr/local/bin/python
print "Content-type: text/html\n\n";
print "Hello World!";

If you copy the sample code from the goneo help to try it out, the quotation marks may be displayed incorrectly and interpreted incorrectly. This would also cause an HTTP error of 500. Therefore, please make sure that you use correct quotation marks in your script.

In addition, we recommend that you use an editor that can change the character encoding of your script to Unix file format. The free editor "notepad++", which you can download from the following website: http://notepad-plus.sourceforge.net, would be suitable for this.
Tip: On this page you can also download a German language file for the editor.

Then click in the editor "notepad++" on "Edit" → "End of line" and select "Convert to Unix (LF)".


I understood that my script must be located in CGI-BIN directory and I have copied it there
[Image: goneo01.png]

Please help, how can I start it and where can I see results of its execution.
Thank you
Reply
#2
http://yourwebpage/cgi-bin/hallowelt.py
Reply
#3
Well there are numerous ways to do it, but it depends on what you want to accomplish.

I’ve recently wrote a Python script, that dumps a MySQL database, and that, additionally, compresses a folder with images, and stores all of that into a tar.gz compressed file, which is then uploaded to Google Drive.

Well you have guessed it. This script is used for backup, and it is ran on a Linux Debian VPS, every day at 00:00 with the use of the built-in crontab feature.

But maybe I have misunderstood you, and you want to actually run that script from a web server backend. If that is your goal, I am going to explain how to do that in Python 3.

Let’s say you have built a Python backend and you want to execute a standalone Python script. You can do that using the os, or subprocess module by either using os.system or subprocess.call. There are more functions in these modules, that can accomplish the same task, but for a different purpose such as to get the output of those scripts and more.

Of course this makes little sense when using a Python backend, since you could have just implemented that script, as part of the backend. If you are using another language such as Java and the Spring framework, then you would not have the possibility of implementing it as Java code, but you would have to actually call it from that app. If you want to accomplish that, then just search online on how to execute shell commands from your favorite language.

All in all, it is very straightforward to call a Python script from a web server, in any possible scenario.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020