Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

trying to create simple py script

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


smaran.harihar at gmail

Aug 9, 2012, 2:52 PM

Post #1 of 5 (808 views)
Permalink
trying to create simple py script

Hi Guys,

I am trying to create a simple cgi-script to receive a Ajax
call, manipulate the string received and send it back as JSON.
Most of the people I have spoken to, seemed to be against using the cgi
script, but most of the documentation and tutorials seem to point to cgi
for AJAX calls. They said, it makes more sense to use django or bottle
which are python web framework.

So I am confused and wanted some guidance as to which should I chose?
Should I go with cgi or django/bottle?

--
Thanks & Regards
Smaran Harihar


lutz.horn at fastmail

Aug 9, 2012, 11:35 PM

Post #2 of 5 (792 views)
Permalink
Re: trying to create simple py script [In reply to]

Hi Smaran,

Am Do, 9. Aug 2012, um 23:52, schrieb Smaran Harihar:
> I am trying to create a simple cgi-script to receive a Ajax
> call, manipulate the string received and send it back as JSON.

I can recommend bottle. The following example manipulates a JSON request
body and returns it. That is *much* easier than using CGI.

#!/usr/bin/env python

from bottle import request, post, run

@post('/hello')
def index():
if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
body = request.json
body["baz"] = "qux"
return body
else:
return 'This is a normal HTTP Post request.'

run(host='localhost', port=8080)

Lutz
--
http://mail.python.org/mailman/listinfo/python-list


iftecan2000 at gmail

Aug 10, 2012, 12:49 AM

Post #3 of 5 (791 views)
Permalink
Re: trying to create simple py script [In reply to]

I have been using Flask for a while and it's been a positive experience so
far. It's simplicity helps you to get things done faster.


On Fri, Aug 10, 2012 at 8:35 AM, Lutz Horn <lutz.horn [at] fastmail> wrote:

> Hi Smaran,
>
> Am Do, 9. Aug 2012, um 23:52, schrieb Smaran Harihar:
> > I am trying to create a simple cgi-script to receive a Ajax
> > call, manipulate the string received and send it back as JSON.
>
> I can recommend bottle. The following example manipulates a JSON request
> body and returns it. That is *much* easier than using CGI.
>
> #!/usr/bin/env python
>
> from bottle import request, post, run
>
> @post('/hello')
> def index():
> if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
> body = request.json
> body["baz"] = "qux"
> return body
> else:
> return 'This is a normal HTTP Post request.'
>
> run(host='localhost', port=8080)
>
> Lutz
> --
> http://mail.python.org/mailman/listinfo/python-list
>


drsalists at gmail

Aug 10, 2012, 11:40 AM

Post #4 of 5 (790 views)
Permalink
Re: trying to create simple py script [In reply to]

CGI's old stuff. Sure it's easy to find doc about it - it's been around
longer.

I'd recommend either CherryPy or Bottle - because these are the two (that I
know of) that support Python 3 today.

Here's a nice comparison of Python REST frameworks:

http://www.youtube.com/watch?v=AYjPIMe0BhA

I'm using CherryPy on Python 2.7 with the ruby-on-rails-like "Routes"
dispatcher.

Bottle does sound pretty nice.

On Thu, Aug 9, 2012 at 9:52 PM, Smaran Harihar <smaran.harihar [at] gmail>wrote:

> Hi Guys,
>
> I am trying to create a simple cgi-script to receive a Ajax
> call, manipulate the string received and send it back as JSON.
> Most of the people I have spoken to, seemed to be against using the cgi
> script, but most of the documentation and tutorials seem to point to cgi
> for AJAX calls. They said, it makes more sense to use django or bottle
> which are python web framework.
>
> So I am confused and wanted some guidance as to which should I chose?
> Should I go with cgi or django/bottle?
>
> --
> Thanks & Regards
> Smaran Harihar
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


smaran.harihar at gmail

Aug 10, 2012, 11:51 AM

Post #5 of 5 (789 views)
Permalink
Re: trying to create simple py script [In reply to]

Sorry forgot to update. I did change the port to 8000 and now server is
running but I am getting a 404 page.

http://128.196.142.94:8000/bottle/hello.py

I have the server up right now you can see.

Thanks,
Smaran

On Fri, Aug 10, 2012 at 11:35 AM, Smaran Harihar
<smaran.harihar [at] gmail>wrote:

> Hi Lutz,
>
> Sorry forgot to update. I did change the port to 8000 and now server is
> running but I am getting a 404 page.
>
> http://128.196.142.94:8000/bottle/hello.py
>
> I have the server up right now you can see.
>
> Thanks,
> Smaran
>
>
> On Fri, Aug 10, 2012 at 11:28 AM, Lutz Horn <lutz.horn [at] fastmail> wrote:
>
>> Hi,
>>
>> Am 10.08.2012 um 19:10 schrieb Smaran Harihar:
>> > python hello.py but I got this traceback error.
>>
>> Use a different port in this line:
>>
>> > run(host='localhost', port=8080)
>>
>> For example 8090 or 8081 or
>>
>> Lutz
>
>
>
>
> --
> Thanks & Regards
> Smaran Harihar
>
>


--
Thanks & Regards
Smaran Harihar

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.