Using python as a server-side script

Using python as a server-side script

Postby mayoarm » Sat Jun 22, 2013 10:08 am


I wish to use python as a server-side script similar to PHP to write a file locally on the server. But obviously, when simply accessing the python script through the URL it doesn't run it, but instead tries to download it.

Fully, what I want is for the python script to receive parameters through the POST method and use them to manipulate changes on the server.

Thanks in advance for any help.
Posts: 1
Joined: Sat Jun 22, 2013 10:01 am

Re: Using python as a server-side script

Postby stranac » Sat Jun 22, 2013 12:16 pm

What you're describing sounds like CGI or WSGI. The latter is more popular and probably better.

However, it is very unusual to access python files directly, and most people prefer to use a web framework.
For small applications, a micro framework, such as flask, is probably the best option.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
Posts: 1097
Joined: Thu Feb 07, 2013 3:42 pm

Re: Using python as a server-side script

Postby micseydel » Sat Jun 22, 2013 10:42 pm

Python Server Pages are a use of Python like PHP as well, and are good for small pages although frameworks are definitely better for websites.
Join the #python-forum IRC channel on!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
Posts: 1223
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Using python as a server-side script

Postby ochichinyezaboombwa » Tue Jun 25, 2013 2:44 am

In order to make your python scripts run as cgi scripts in the context of a web server, you need to configure it (the web server). Do you know how to? It's exactly the same for PHP or Python (changing the config:
Code: Select all
    Options Indexes FollowSymLinks MultiViews ExecCGI
    AddHandler cgi-script .cgi
setting right permissions, etc.).

Furthermore, to make a Python script useful in the Web context, use the cgi module, specifically cgi.FieldStorage. That's how your script will send and receive data (read about it).

As for the method, you simply specify it as a parameter of the form, e.g.:
Code: Select all
f_name = "form1"
f_method = "POST"
script_name = "my1st_cgi.cgi"
print '<FORM name=\"%s\"  METHOD="%s" ACTION="./%s">' % (f_name, f_method, script_name)
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Return to Web Development

Who is online

Users browsing this forum: No registered users and 3 guests