So I recently discovered mod_python, which is great because its not hard to rewrite the PHP code I had and execute through Apache. However, I just found out that my hosting service, (HostGator), does not support this module because they have a single apache user instance per physical shared server.
This begs the question:
Without the use of something like Django, Pylons, etc, is there a way to invoke python routines server side from HTML code and have it return web content?
Example:
Simple form
<form name="message" action="directory/pythonscript.py/function" method="POST">
where, upon submission, the form parses the content in the form, and then the python routine returns a string of the newly assembled HTML file based on the input of the form, and the browser presumably re-renders it.
def function(args):
#parse inputs
#assemble html string
return htmlString
Additional question:
With mod_python, you must pass at least the "req" arg, which points to the apache request structure. Is this functionality still present without mod_python? Would I be able to somehow physically map to that without mod_python?
I'm presuming that all of this is possible because HostGator does support python and a lot of the modules, smtpd for example, have functionality to send emails, communicate over the web, etc, so presumably there is some way to access this API independent of Apache???
99% of searches for this return info about Django, Pylons, etc. If that is indeed the best way to do it, I will explore that option, but all I want to do for now is create some dynamic HTML content with a back end python handler without having to install any other APIs.
Thanks.
EDIT
I should clarify that I am not attached to mod_python at all, it is just something I found. My question is geared towards avoiding non-standard, (standard = shipped with Python 2.7.x, useable through apache or otherwise), APIs if possible.
After some digging, it seems that the quickest way to do this is through php, but not as per the previous answer.
In my HTML I should have:
<html> <form name="form" method="$POST" action="formAction.php" </html>
in formAction.php:
<?
system( 'python ~/scripts/pythonscript.py/function', $return)
echo $return
?>
where $return is the dynamically generated HTML page string. Within the python script, I'll have to parse the posted values from the HTML session somehow. I think that answers my question but I'll wait and see if someone else posts something more interesting.
EDIT: Figured out how to parse the POSTed values:
<?php
$VAR1 = $_GET["var1"];
$VAR2 = $_GET["var2"];
system("python <script>.py -args=[".$VAR1.",".$VAR2"."]", $return)
echo $return
?>
for example.
You could do a php.execute('pythonScript.py') on submit, right?
Related
So I am attempting to use bottle.py and twitter bootstrap together to make a small website. I need to be able to insert a reasonable amount of data at various points in the HTML using Python but I am not really sure I understand how the HTML and python are communicating.
Here is an example of twitter and bottle working together.
He mentions linking a couple of .js files in the html and I can see where he does that but I am not really sure how that affects how the python interacts with the html. Is there a callback from the Javascript that the python catches using request.GET.get().strip():?
Also one of the last lines is:
return template('templates/gpio.tpl', colour1=colour1, colour2=colour2, colour3=colour3)
I am not sure how the templates/gpio.tpl is connected to the html he mentions below. I understand that the colour# variables are referenced in the html (I assume this happens with the {{}} syntax) but I am not sure how the html gets called at all.
From what I understand (which so far isnt a whole lot) this is how it goes:
User enters "server:port/gpio" into a webbrowser
The python decorator gets called by bottle and the function is run returning the template at the bottom.
This is where I get confused.
A) How does the python script know to call the html?
B) How does the gpio.tpl template code get sent to the html?
C) Is it safe to assume that the python arguements sent to the template function can be referenced using the {{}} syntax or is there more to it?
D) How does the html call back to the python to update the buttons he shows at the bottom?
D.1) does the JS linked at the top have something to do with this?
Lastly: If anyone has a another/better example of linking bootstrap and bottle I would be very happy to see it.
This is quite a loaded post. Thank you for your patience. :D
You'd really need to first learn how the HTTP protocol works... But let's try to quickly answer your main question:
how the HTML and python are communicating
Quite simply: they don't. What happens is:
your client (usually your browser) send an HTTP request to your site
the front web server (Apache, Nginx, whatever) sends this request to the bottle.py application
the bottle.py app dispatch the request to the right controller function matching the url's path portion and request's method against the defined routes)
the controller function does what it has to do and returns an HTTP response to the front web server
the front web server send this response to your client
Usually - but not necessarily - the HTTP response contains HTML content, generated by the controller using a template. IOW : bottle.py uses the template to generate html that is sent back to the client.
Once the response is sent, there's no more "communication" until the client sends another request.
So if I wanted to make an html button that changed something on the page how would I send that response back to bottle.py regenerate the page with the change?
It depends on what you want to change...
For example, pushing the button could trigger a new HTTP request, you should then define a new feature in your code.
Let's take the previous example, and imagine you want to add a switchOff button.
You have to add the following button somewhere in the gpio.tpl :
<input type="submit" class="btn" name="LedsOff" value="Turn off the leds!">
Then, modify the function gpio() to add a new condition with the following :
elif request.GET.get('LedsOff','').strip():
from quick2wire.gpio import Pin, exported
with exported(Pin(12, Pin.Out)) as out1, exported(Pin(13, Pin.Out)) as out2:
out1.value = 0
out2.value = 0
I'm trying to accomplish a little bit of automation which includes submitting a form on a webpage. The values for the form are already coded per item in the list.
I've tried many different modules with Python and nothing seems to give me an answer. I don't have access to Visual Basic and I've personally never dealt with .aspx pages before.
This is the Form name
And I thought I was set and ready to go when I found the parameters for the form:
function ShowEditForm(id, param1, param2, param3, param4) #actual parameter names removed for security
And this is the part that's the major headache:
<INPUT id=__EVENTTARGET type=hidden name=__EVENTTARGET> <INPUT id=__EVENTARGUMENT type=hidden name=__EVENTARGUMENT> <INPUT id=__VIEWSTATE type=hidden value=/wEPDw... #This continues for 800+ characters
I believe this is the cause of my failure of code, am I on a witchhunt trying to post to an .aspx form in python?
Thanks
you would need to parse/parameterize your post headers and contents. this can be non-trivial.
check out mechanize for access at the HTTP level, with some form handling convenience.
check out selenium, for driving a real browser in Python.
I don't think aspx has anything to do with it.
Have you tried http://pypi.python.org/pypi/selenium ?
Indeed, the server-side handling of the POST request won't work if those hidden values aren't present. ASP.NET uses that stuff to track statefulness across multiple requests. Reverse-engineering ASP.NET Web Forms HTTP requests isn't a fun endeavor.
You'll probably need to request the page, scrape the hidden values it gives you, and include those in the POST.
Stepping through a manual interaction with the page and capturing requests/responses in something like FireBug will also give you a good idea of the values being sent back and forth between the client and the server. It wouldn't surprise me if there's some JavaScript emitted to the response which dynamically modifies some hidden fields in server-pre-determined ways as well, helping to indicate which button was pressed or which control was in some way modified.
Asp.net has a feature called viewstate (encrypted page state settings) which you can't fake, and which the page may be using by default and will expect to see on post to the form when submitting back to itself (called post back).
If you control the .aspx code it likely has an associated .cs or .vb file with the code to do the form processing. You can change the code to get values from posted form or URL parameters instead of (or in addition to) controls on the original form. If the site is compiled and you don't see any .vb or .cs files to change you would need to locate the original source files for the solution.
I would like to change the logo of a website based on which menu is currently activated/seen by the user browsing the website.
For instance I have www.urltowebsite.com/menu1 = Header Logo 1
And then I have www.urltowebsite.com/menu2 = Header Logo 2
And on top of this I want to add an else statement stating that: If any other menu is selected, use header logo 3.
How can I make this possible with Python? I cant seem to wrap my head around what to define where and how to call up the different functions on the HTML website.
Oh and I insist doing this with Python. And preferably without any framework such as Django. But if needs be I can install web.py
EDIT:
Am I forced to go with php then? I would like to once and for all start utilizing Python on my web projects.
The website is made in simple HTML as I said first. The Javascript functions are only used to serve the HTML menu's through AJAX. Again this does not matter much for what I am trying to do, as menu's have classes and I can define those in php and thus change my logo/header.
What I want to do is to use Python in this instance. Here is a code snippet from the site:
<div id="header">
<span class="title"><img src="http://www.url.com/subfolder/images/logo.png"/>
</span>
</div>
And some more relevant to this:
<div id="menu">
<ul>
<li>001</li>
<li>002</li>
<li>003</li>
<li>004</li>
<li>005</li>
<li>006</li>
<li>007</li>
<li>008</li>
</ul>
</div>
So can I use python here?
You're asking to do the wrong thing the wrong way.
In order to change the logo based on the URL in Python , you need Python to generate the page and know what that url is.
There are two ways to do that in Python:
Use an existing Web Framework
Write your own Web Framework
"Python" doesn't know or care what your URL is - the frameworks and support libraries ( Django, Pyramid, Bottle, Flash, Tornado, Twisted, etc) figure out what the URL is by an integration with an underlying web server ( though some have their own webserver coupled in ). Similarly, PHP doesn't really know or care what the URL is - that information comes from an integration with Apache or FCGI/Nginx/etc. PHP tends to ship with most/all of that integration done. It's also worth noting that PHP is not just a language, but a web framework. Python is just a language.
Most Python frameworks will be written to the WSGI spec and have a "request" object that has all the data you want ( and many use the WebOb librbary for that ).
If you plan on doing everything with static HTML files, then you have a few options:
have a single static directory. use javascript to figure out the addressbar location, and render the corresponding logo / write the headers & footers.
have a "template" directory of all your HTML. use a Python script build a static version of each website with the custom headers/footers and configure your webserver to serve a different one for each domain.
No, Python cannot run inside the HTML web page. If you're really serving plain HTML pages then you must use javascript to execute code in the browser once the page is loaded. However, since you mention using AJAX, it sounds like it's not really true that you're serving plain HTML but rather have some server side code. If so, that server side code is the place to put your HTML-construction logic. To know the best way to do that, you would have to describe what's happening on the server.
Although I haven't used it, I have heard that the pyhp project more or less provides php-like embedded functionality for python.
I have a html page that contains a form and I would like a user to be able to input some data in the form. I would then retrieve this data with a python script.
I don't understand how to make the html and python communicate. How can the python retrieve the form after the user has clicked on post? Is there a basic example I can use to demonstrate this?
What you need is an HTTP server. Python can be used to make a server, or you could get one such as Apache or Lighttpd. I wrote an example application (Rock, Paper, Scissors) based off of this a while ago: https://dl.dropboxusercontent.com/u/95437705/RPS_Web.tgz
C0deH4cker is right that you should have your python program running somewhere in some kind of web server.
A proper way to do this for larger projects is to use a web framework like Django or Web2py. These frameworks have excellent documentation and tutorial pages that are well worth the effort of consuming. A smaller benefit is that both have their own built-in webserver for development an small deployment.
Another way is to write python code that has a web server of its own. You could have a look at Twisted, the networking framework for python. Good info is at http://twistedmatrix.com/documents/current/web/howto/web-in-60/index.html.
Finally -- but this is considered a bit outdated, I guess (??) -- there is the CGI option you describe. Most web servers like apache and lighttpd have the option to run CGI scripts. CGI scripts are very simple programs that, in their simplest form, when run, output HTML code on their stdout. The webserver looks at the first ("shebang") line in the script to determine the script interpreter, executes the script, picks up the output and serves it over HTTP.
Specific provisions have been made in CGI for parameter passing. In the Python CGI module, these are obtainable through cgi.FieldStorage()
First for some HTML:
An HTML form has an "action" and a "method" attribute. Action is the URL (absolute, or relative to the current path) where it will send the form data to; Method is the method to use, usually for a form with a submit button this is "POST".
So if you have this HTML code running somewhere:
<html>
<body>
<form name="sample" action="cgi-bin/myscript.cgi" method="post">
Name: <input type="text" name="name"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
When the user presses the submit button, all input values are posted to the URL "myprogram.cgi".
A good text on what CGI code should look like is in http://docs.python.org/library/cgi.html .
Some code that should work is:
import cgi
print "Content-Type: text/html" # HTML is following
print # blank line, end of headers
print "<TITLE>CGI script output</TITLE>"
print "<body>\n"
print "<H1>This is my first CGI script</H1>"
print "Hello, world!"
form = cgi.FieldStorage()
if "name" not in form:
print "<H1>Error</H1>"
print "Please fill in the name fields"
else:
print "<p>name:", form["name"].value
print "</body>"
Now, it is important that you realize that you have to make this code available on the specified URL from a web server. How you should do this depends on your web server.
On apache under linux, you would normally put the script in /var/www/cgi-bin. Possibly you should enable cgi execution in the apache configuration, as it may be switched off for security reasons.
I am just starting Python and I was wondering how I would go about programming web applications without the need of a framework. I am an experienced PHP developer but I have an urge to try out Python and I usually like to write from scratch without the restriction of a framework like flask or django to name a few.
WSGI is the Python standard for web server interfaces. If you want to create your own framework or operate without a framework, you should look into that. Specifically I have found Ian Bicking's DIY Framework article helpful.
As an aside, I tend to think frameworks are useful and personally use Django, like the way Pylons works, and have used Bottle in the past for prototyping—you may want to look at Bottle if you want a stay-out-of-your-way microframework.
One of the lightest-weight frameworks is mod_wsgi. Anything less is going to be a huge amount of work parsing HTTP requests to find headers and URI's and methods and parsing the GET or POST query/data association, handling file uploads, cookies, etc.
As it is, mod_wsgi will only handle the basics of request parsing and framing up results.
Sessions, cookies, using a template generator for your response pages will be a surprising amount of work.
Once you've started down that road, you may find that a little framework support goes a long way.
You will have to look into something like CGI or FastCGI, which provides an API to communicate to the webserver.
Google App Engine enables you to write simple apps, and even provides a local webserver where you can try things out.
People here love frameworks. One shortcoming I have noted is that Python lacks a handy-dandy module for Sessions as a library built-in, despite it being available in PHP and as CGI::Session in Perl.
You will end up doing:
import cgi # if you want to work with forms and such
import cgitb; cgitb.enable() # to barf up errors to the web
print 'Content-type: text/html\n\n' # to start off any HTML.
You will have to write session stuff on your own.
For a PHP programmer, I think mod_python is a good way to get started without any framework. It can be used directly as Apache 2 module. You can have code tags (like <? ?> in PHP) and even conditional HTML output (HTML inside if statement):
<%
if x == y:
# begin
%>
... some html ...
<%
# end
%>
(simplified example taken from onlamp.com's Python Server Pages tutorial)
You should try web.py, it provides a bare minimum of features that does not get in your way.
http://webpy.org/
You can just make the entire thing yourself as a CGI script written in python. On an Apache server you go into the httpd.conf file and add these two lines at the bottom.
AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict
Then the standard output is redirected to the client, i.e. the print(...) method sends text to the client. Then you just read the .html, .css, and .js files stored on the server and print() each line. Connect to your database on the backend. Set up your security/authorization protocols... Basically you will need to make the entire framework yourself, only it will be customized to fit your needs perfectly.
Probably a good idea to come up with some special character to parse for when reading the files on the server and before printing to insert any dynamic content, such as:
HTML
<div>
<p> <<& pythonData $>> </p>
</div>
Python
htmlFile = open("something.html", "r")
for line in htmlFile:
if "<<&" in line:
# figure out what characters that special symbol is in the line
# replace them with dictionary value or variable or something
print(line)
else:
print(line)
Here is the documentation for the official library to work with Common Gateway Interface (CGI) in python: https://docs.python.org/3/library/cgi.html It includes an example that shows reading form data sent to the server into a python script.
Don't forget to tell your scripts where the python interpreter is on the Apache server (should be in /bin somewhere), in other words point at python with the sh-bang:
#!/bin/python3.10
Or wherever your server's python interpreter is located at.