4

Is it possible to execute a python-script on a server without using something like django?

I mean I put script.py on host.com and want to call it like this:

    http://www.host.com/script.py

The script then does something like calculating some variables and saving them on a mysql database.

edit: I assume I have to use something like cgi :-\

2
  • 3
    I wrote an answer to this once: stackoverflow.com/a/9382853/916657 Commented Mar 5, 2012 at 19:19
  • using apache with webmin and virtualmin. Commented Mar 5, 2012 at 19:31

2 Answers 2

1

In short, yes. http://wiki.python.org/moin/CgiScripts. You'll have to either put your scripts in a cgi-bin folder or adjust the configuration for your web server.

Sign up to request clarification or add additional context in comments.

3 Comments

well. how about just using a php-wrapper to call my script using exec. script.php -> exec("script.py")
@xMRW You would need to do exec("python script.py"); at the very least, as python scripts are interpreted.
@xMRW You can do that, if you make your script executable (chmod +x filename.py on unix). However, Lattyware's solution is more portable (You can't make python scripts executable on windows) and probably the better one, especially if your site is hosted on someone else's server. Many server admins won't want you making your scripts executable, for security reasons. If it's your own server, and it's unix (or some derivative) either way works.
0

It's possible with httpout. It's lighter and simpler than the CGI approach.

  1. Install httpout
python -m pip install httpout
  1. Create a hello.py in my_public_folder/
# hello.py

print('<p>Hello, World</p>')
  1. Serve
python -m httpout --port 8000 my_public_folder/

Then go to http://localhost:8000/hello.py

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.