How to send a post submit to web server?

This is the place for queries that don't fit in any of the other categories.

How to send a post submit to web server?

Postby iamnobody » Wed Feb 20, 2013 9:56 am

Hi,

I have a web server page http://myserver.com/server.php to receive $_POST["query"], appreciate an example to make a python script as a client to submit query string to the server http://myserver.com/server.php?

Thank you.
iamnobody
 
Posts: 4
Joined: Wed Feb 20, 2013 9:47 am

Re: How to send a post submit to web server?

Postby stranac » Wed Feb 20, 2013 10:48 am

Well, the easiest way to send http requests is using the third party requests library.
With it, it's as simple as:
Code: Select all
import requests

r = requests.post('http://myserver.com/server.php', data={'query': 'whatever'})

But if you want to do it using the standard library, it's not much more complicated.
Code: Select all
import urllib
import urllib2

data = urllib.urlencode({'query': 'whatever'})
r = urllib2.urlopen('http://myserver.com/server.php', data)

Note: there's an urlopen() in urllib as well, but it looks like it's been deprecated, according to the docs.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: How to send a post submit to web server?

Postby iamnobody » Thu Feb 21, 2013 11:18 am

stranac wrote:.....
But if you want to do it using the standard library, it's not much more complicated.
....


Thank you very much stranac, that's great help.

Cheers.
iamnobody
 
Posts: 4
Joined: Wed Feb 20, 2013 9:47 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests