Running a Python Script on my website? - python

I have a website with miniwebhost.com, which supports python. I want to have a page that runs one of my text based games I have made on Python. So, how would I go about doing it? I know I have to make it executable and something about a cgi-bin(which I have). Put your answer in clear steps please.
Site is: www.rosshudson.co.uk/

Python web applications are often hooked to the web server (e.g. Apache httpd) using the WSGI module. Note that your app need to handle the HTTP requests correctly either by using a framework (like Django) or the BaseHttpServer, BaseHttpRequestHandler from the standard library.

Related

Use external python libraries on Pyramid

Can I use any external libraries that are developed for python on Pyramid?
I mean is it the 'normal python' to which I can import external libraries as I do with the standard python downloaded from python.org
What is the situation for Django and Flask and Bottle?
My intention is to create backend for a mobile app. I want to do it specifically in Python because I need to learn python.
The app is a native android app. Therefore the there is no need to use response with nice html code.
I just want Django/Flask/Pyramid to direct http request to relevant python functions. Everything else including user auth, database is handled by my code I write. Is there a better more simpler way to map http request/responses with the relevant functions without using these 3 platforms?
In case I use one of these can I still use my own libraries?
Yes, all of those frameworks are simply running Python code to handle requests. Within limits you can use external libraries just fine.
The limits usually are dictated by the WSGI server and the nature of HTTP requests; if your library changes the event model (like gevents) or relies heavily on changing the interpreter state (global state, localization) or takes a long,long time to produce results, then you may need to do more work to integrate.

What is considered the current industry standard alternative to CGI for Python web development?

For one of my classes last semester I had to create a website using a CGI script. While it was a good introduction to web development, but I can see it has it's limitations. So my question is, in the industry today, what is the most common approach for parsing a Python script into html?
I have found a few posts on Stack Overflow that suggest FastCGI for PHP, but I am wondering if the same answers apply to Python.
I assume that I would use .py rather than .cgi? If that's the case, how would a web server know to run it?
This is a old way of thinking :)
Nowadays you start your Python process, and it begins to listen on a port. The Python process itself contains a webserver and is almost completely self-contained - see here for the different webservers you can use within your Python process - gunicorn is a popular one.
Of course a lot of people still put Nginx or Apache in front of their Python process as a "reverse proxy". Essentially this is to handle SSL/TLS but also sometimes to do load balancing, custom error pages if your Python process is down etc.
WSGI is a protocol that describes (synchronous) communication between a web server and a web application. https://en.wikipedia.org/wiki/Web_Server_Gateway_Interface

Is it possible to run python in the web with github.io?

I want to run a python file in the web I have in a GitHub repository. Is it possible to do this?
And by running in the web, I mean putting #!/usr/bin/python and print 'Content-type:text/html\n in the first two lines.
In general this is not possible, Github (pages) serves only static content (ex: HTML, CSS, JS). If you want python to run (ex generate dynamic content) you need a web server capable of running python (your browser were the contents of GitHub Pages get downloaded and run can't do it).
That said there are experimental ways of running subsets of python in the browser. Take a look for a example at this question.
If you truly want to generate a complete HTTP response via standard output, then start by reading about CGI and Python's standard cgi module. You'll also need to have access to a CGI-compatible web server, perhaps running on a virtual host.
However, CGI is quite obsolescent as a way to produce dynamic output for the web. #jjwon's suggestion to look at Python-based web application frameworks like Flask is a good one.

adding browser capability to an existing python script

In essence I want to run existing python scripts through a web browser, display the text and plots(if any) and also keeping the ability to run them through the command line.
I am wondering is there any toolkit that can help me with the development.Also it would be nice if the toolkit does or allows JavaScript based interactive plots.
Thanks!
-Abhi
WSGI is designed for just this purpose - it provides an interface for a web server to initiate python scripts.
You probably don't want to work with WSGI in the raw. Flask is a straightforward, simple framework you might use for this.
The details of how to actually build a WSGI web server are well beyond the scope of a stackoverflow answer - you can find plenty of tutorial docs on Flask's website.

cgi scripts with no server

My goal is to use to make it easy for non-programmers to execute a Python script with fairly complex options, on a single local machine that I have access to. I'd like to use the browser (specifically Safari on OS X) as a poor man's GUI. A short script would process the form data and then send it on to the main program(s).
I have some basic examples of python scripts run using the built-in Apache server, by clicking submit on a form whose html is th:
e.g. here. What I want to do now is do it without the server, just getting the form to invoke the script in the same directory. Do I have to learn javascript or ...? I'd be grateful for any leads you have. Thanks.
It doesn't make sense -- what a browser does when it submits a form by definition is to make a request to a web server.
If all that's going on is that you don't want to be running Apache, you could hook something simple up using the CGIHTTPServer class that's provided as part of the Python Standard library.
If you don't want a server process at all, and you're using a suitably modern browser, you may want to look at using HTML5 local storage, but that's not a Python solution.
Well, there always has to be some kind of "server" involved to communicate over HTTP. You could have a python script listening on port 80 on your machine, that in turn runs the scripts specified with the form's action attribute.
You won't get away without some sort of server, I'm afraid.
PS: There are already a couple of good minimalistic python HTTP servers that would do the trick. Just google for it.
Regards, aefxx
Pyjamas Desktop will allow you to deploy a browser-based desktop application.

Categories

Resources