2

I'm new to using Python in a web development environment (I've previously just used it to write scripts I run on the command line). I'm hoping to send some input parameter from an HTML page via AJAX, run a python script, and then send back to the webpage a JSON object generated by my python script. I'm trying to get this example running just to get a sense for how this works:

http://davidderiso.com/post/6168199987/using-python-and-jquery#disqus_thread

I'm getting this error message from test.py:

Failed to load resource: the server responded with a status of 501 (Unsupported method     ('POST'))

Do I need to be running this python script with something like flask or bottle? Sorry, I'm a total newbie when it comes to this stuff!

Thanks!

1
  • If you give us more feedback on what your stack looks like we could help you. The request cycle will look something like this: 1) Server receives GET request for the document 2) Server launches python process based on the variables passed to the GET request 3) Result is computed 4) Another document is updated with the result of the computation 5) A separate user tries to access the document and
    – htmldrum
    Commented Jun 13, 2013 at 18:07

2 Answers 2

0

That tutorial is supposed to be showing you how to do it without a framework, but it's quite incomplete. For example, it doesn't talk about how you should be accessing the HTML file in the first place. If you simply open it as a file in your browser, you'd probably get that error: you need it to be served by Apache (which as he does point out is built in for OSX) - http://localhost/my_html_file.html might work.

4
  • I am running a SimpleHTTPServer and accessing the HTML file on some port.
    – araspion
    Commented Jun 13, 2013 at 18:24
  • That won't work. For a start, the default class doesn't handle POSTs (hence the error). Secondly, it doesn't handle CGI, so it will just serve your Python file as text. You'd need to use CGIHTTPServer. Commented Jun 13, 2013 at 18:39
  • Thanks, this looks useful. This documentation is a wee bit above my head -- any chance you could point me in the direction of some example usage?
    – araspion
    Commented Jun 13, 2013 at 20:28
  • @araspion you could try pymotw.com/2/BaseHTTPServer/index.html#http-post , the site has great examples of most (~80%) python stdlib modules. for you specifically, while does not cover CGIHTTPServer, but do show a POST example with BaseHTTPServer and cgi.
    – n611x007
    Commented Aug 17, 2014 at 19:33
0

I think you should use the Tornado Web Server ! Using Tornado you would be able to have your python code alongside your html code.

Something like this ...

{% code %}
import pandas as pd
header = 'Using Tornado'
data = pd.read_csv('country_data.csv')
....
{% end %}

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>{{ header }}</title>
<style>
...
<body>
...
</html>

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.