Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Python scripts like PHP

There are Apache 2 + mod_wsgi + Python 2.6 on server.

I would like to run scripts from Python like PHP scripts. The idea may seem silly, but I'm so accustomed (at least at first learning Python).

Example:

PHP - http://example.com/script.php

Python - http://example.com/script.py

P.S. I know about mod_rewrite and other similar tricks that can do that. But it is only a disguise, and not a direct run.

UPD: My .htaccess file. index.py works, but other python scripts get 404 error.

<Files *.py>
SetHandler wsgi-script
Options ExecCGI FollowSymLinks
</Files>
 DirectoryIndex index.py
like image 490
Ticksy Avatar asked Mar 12 '12 09:03

Ticksy


People also ask

Can you use Python like PHP?

Easy to LearnPython and PHP are very similar. Both are object-oriented interpreted languages that run on a variety of operating systems. They're both dynamically typed and have terrific IDE support. Lastly, Python and PHP both stand out in the world of syntax.

Can PHP call Python script?

To call a Python file from within a PHP file, you need to call it using the shell_exec function.

How do I run a Python script in production?

To deploy, you need to upload this artifact to your production machine. To install it, just run dpkg -i my-package. deb . Your virtualenv will be placed at /usr/share/python/ and any script files defined in your setup.py will be available in the accompanying bin directory.

Can you mix PHP and Python?

Yes it is possible.


1 Answers

That doesn't look as cool as having a wsgi app running, so I recommend that you use the flask framework which is as simple as can be a sane framework.

Here's a link describing the install procedure on mod_wsgi.

Later on, you might want to consider a cool framework like Django, Pyramid, Grok ...

If you really want to use mod_wsgi like mod_php check Graham Dumpleton's great answer.

like image 194
jpic Avatar answered Sep 28 '22 10:09

jpic