How to setup and run Python on Wampserver?

一世执手 提交于 2019-12-04 02:11:45

Do not use mod_python; it does not do what most people think it does. Use mod_wsgi instead.

How about using web.py (download) or django?

They have their own web server, and you can also connect MySQL server with MySQLdb extension.

Recognising that the post asks about mod_python, I'm posting the following, in case using CGI is acceptable.

It has been a while since I got this to work, but I got CGI scripts written with Python to run under Wampserver with a couple simple things (although it didn't seem simple at the time):

  • Download and install Python if you haven't already. The standard install should let you run programs from a command prompt (which you'll need).
  • Write your Python CGI program, making the first line be #!python (or the full path to the python executable). While the first line isn't usually necessary for Python programs under Windows, Apache seems to need this so it knows the program is Python.
  • Place the program in your cgi-bin directory.

That should do it. I double checked my httpd.conf file and don't see any changes to get Python working. (This assumes you already have CGI working otherwise.)

The following simple script should tell you if you have things working:

#!python
print "Content-type: text/html"
print ""
print "<html>"
print "<head>"
print "<title>CGI Test of Python</title>"
print "</head>"
print "<body>"
print "This is a test"
print "</body>"
print "</html>"

My WSGI setup made on WAMP server 2.5, 32bits (Apache 2.4.9 32bits) with PythonWin 2.7.8 (default, Jul 2 2014, 19:50:44) [MSC v.1500 32 bit (Intel)] on win32 went on the next way.

WAMP route = C:/wamp/

Config Apache

Download 32bits mod_wsgi.so from http://www.apachelounge.com/viewtopic.php?t=5143 and place it as c:\wamp\bin\apache\apache2.4.9\modules\mod_wsgi.so

Load the wsgi module to apache in the main C:\wamp\bin\apache\apache2.4.9\conf\httpd.conf:

LoadModule wsgi_module modules/mod_wsgi.so 
WSGIScriptAlias /API c:/wamp/www/API/code.py 

Get webpy

C:\tmp>git clone git://github.com/webpy/webpy.git
C:\tmp>python webpy\setup.py install

Test it:

C:\tmp>python
ActivePython 2.7.8.10 (ActiveState Software Inc.) based on 
Python 2.7.8(default, Jul  2 2014, 19:50:44) [MSC v.1500 32 bit (Intel)] 
on win32  Type "help", "copyright", "credits" or "license" for more information.
>>> import web
>>>

Create your application as c:\wamp\www\API\code.py

import web
urls = (
  '', 'root',
  '/(.*)', 'hello',
  )

class root:
    def GET(self):
        return "This is the root URI."

class hello:
    def GET(self, name):
        return "Hello %s from webPy." % name

application = web.application(urls, globals()).wsgifunc()

Result

Restart your apache webserver and check http://localhost/API

Step 1: Download the Python Setup https://www.python.org/downloads/release/python-350/

Step 2: Install Python

Step 3: Download the wampserver https://sourceforge.net/projects/wampserver/files/WampServer%202/Wampserver%202.4/

Step 4: Open httpd.conf file in notepad,from that location C:\wamp64\bin\apache\apache2.4.23\conf\httpd.conf

Step 5: find CTRL+F "Directory" in httpd.conf and set wamp install location in Document and Directory , where your wamp server is installed , kindly use forward slash "/" not backword "\"

A.(DocumentRoot "C:/wamp64/www")

B.(Directory "C:/wamp64/www"> )

and Replace these two line inside Directory "C:/wamp64/www">

Remove:-

Options Indexes FollowSymLinks

Add:-

AddHandler cgi-script .cgi .py

Options Indexes FollowSymLinks ExecCGI

C. Set cgi-bin location

(Directory "C:/wamp64/cgi-bin" AllowOverride None Options None...)

Step 6: Restart All Service of wamp.

Step 7: make python program but add these line at first

#!D:/paython installed/python.exe // set path where python is installed

Step 8: save program .py extension.

Step 9: run on browser using

 localhost/file_name.py

Wampserver doesn't have addon for python/django, but XAMPP does.

A good tutorial here:

http://jyotirmaya.blogspot.com/2008/11/xampp-python-django.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!