[Web-SIG] how to post from a cgi script and not a html form??

Alex Botero-Lowry alex at puddlejumper.foxybanana.com
Mon Nov 5 06:10:08 CET 2007


On Sun, Nov 04, 2007 at 08:43:23PM -0800, Jeff Peery wrote:
> Thanks, thats a big help!
>    
>   only two things I don't understand well. when I create a http object with HTTPConnection() do I want this to be to my web host server (hostway.com) or to the server I'm posting to (authorize.net)?
>    
The server you are posting to, it's just basically an HTTP client.

>   and what are the headers used for?
>    
It tells it that the content type is application/x-www-form-urlencoded, which
will be checked for on the remote side.

Alex

>   again, thanks!
>    
>   Jeff
> 
> Graham Dumpleton <graham.dumpleton at gmail.com> wrote:
>   On 05/11/2007, Alex Botero-Lowry wrote:
> > On Sun, Nov 04, 2007 at 05:14:31PM -0800, Jeff Peery wrote:
> > > hello,
> > > I'm pretty new to using python on the web, I've got a bit of code
> > > that works pretty well to get form inputs and such. Now I need to post
> > > some info to a gateway service (for credit card processing) and then
> > > receive their response and do something with it. I can do this no
> > > problem... except that I'm not sure how to post my dictionary (name
> > > value pairs from form inputs i.e., credit card num, expire dates etc)
> > > from the cgi script. I had been using the html forms to submit data to
> > > the server, but now I need to do this from my cgi script. I think this is
> > > pretty straight forward but I didn't see anything in the cgi module.
> > > where do I start, or does anyone have some sample code? thanks!!
> >
> > You'll need httplib which luckily come with the stdlib so no need to install
> > anything.
> >
> > Something like this should get you going:
> >
> > conn = httplib.HTTPConnection(remote_server)
> > values = '&'.join([ '%s=%s' % a for a in values.items() ])
> 
> >From memory, better off using urllib.urlencode() for this as it will
> properly quote and convert special characters.
> 
> > headers={'Content-Type':'application/x-www-form-urlencoded'}
> > conn.request(method, url, values, headers=headers)
> > res = conn.getresponse()
> > data = res.read()
> > return (res.status, res.reason, output)
> >
> > the important bits here are our crappy makeshift
> > application/x-www-form-urlencoded rncoder which
> > is the values line and our setting of the content-type. We also
> > need to make sure the method passed to conn.request is 'POST' or
> > 'PUT' (almost certainly POST) as these are the only ones that accept
> > a body. I think the cgi module may have a better way of doing the
> > encoding, but i've never found it.
> >
> > Alex
> > _______________________________________________
> > Web-SIG mailing list
> > Web-SIG at python.org
> > Web SIG: http://www.python.org/sigs/web-sig
> > Unsubscribe: http://mail.python.org/mailman/options/web-sig/graham.dumpleton%40gmail.com
> >
> 
> 
>  __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com 

> _______________________________________________
> Web-SIG mailing list
> Web-SIG at python.org
> Web SIG: http://www.python.org/sigs/web-sig
> Unsubscribe: http://mail.python.org/mailman/options/web-sig/alex%40foxybanana.com



More information about the Web-SIG mailing list