Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple URL GET/POST function in Python

Tags:

python

http

I can't seem to Google it, but I want a function that does this:

Accept 3 arguments (or more, whatever):

  • URL
  • a dictionary of params
  • POST or GET

Return me the results, and the response code.

Is there a snippet that does this?

like image 942
TIMEX Avatar asked Dec 18 '10 03:12

TIMEX


People also ask

How do you post a URL in Python?

The post() method sends a POST request to the specified url. The post() method is used when you want to send some data to the server.

How do I get post data in Python?

Python is only a language, to get GET and POST data, you need a web framework or toolkit written in Python. Django is one, as Charlie points out, the cgi and urllib standard modules are others. Also available are Turbogears, Pylons, CherryPy, web.py, mod_python, fastcgi, etc, etc.

How do I request a post URL?

POST request in itself means sending information in the body. I found a fairly simple way to do this. Use Postman by Google, which allows you to specify the content-type (a header field) as application/json and then provide name-value pairs as parameters. Just use your URL in the place of theirs.


2 Answers

requests

https://github.com/kennethreitz/requests/

Here's a few common ways to use it:

import requests url = 'https://...' payload = {'key1': 'value1', 'key2': 'value2'}  # GET r = requests.get(url)  # GET with params in URL r = requests.get(url, params=payload)  # POST with form-encoded data r = requests.post(url, data=payload)  # POST with JSON  import json r = requests.post(url, data=json.dumps(payload))  # Response, status etc r.text r.status_code 

httplib2

https://github.com/jcgregorio/httplib2

>>> from httplib2 import Http >>> from urllib import urlencode >>> h = Http() >>> data = dict(name="Joe", comment="A test comment") >>> resp, content = h.request("http://bitworking.org/news/223/Meet-Ares", "POST", urlencode(data)) >>> resp {'status': '200', 'transfer-encoding': 'chunked', 'vary': 'Accept-Encoding,User-Agent',  'server': 'Apache', 'connection': 'close', 'date': 'Tue, 31 Jul 2007 15:29:52 GMT',   'content-type': 'text/html'} 
like image 180
bakkal Avatar answered Sep 21 '22 12:09

bakkal


Even easier: via the requests module.

import requests get_response = requests.get(url='http://google.com') post_data = {'username':'joeb', 'password':'foobar'} # POST some form-encoded data: post_response = requests.post(url='http://httpbin.org/post', data=post_data) 

To send data that is not form-encoded, send it serialised as a string (example taken from the documentation):

import json post_response = requests.post(url='http://httpbin.org/post', data=json.dumps(post_data)) # If using requests v2.4.2 or later, pass the dict via the json parameter and it will be encoded directly: post_response = requests.post(url='http://httpbin.org/post', json=post_data) 
like image 45
ropable Avatar answered Sep 21 '22 12:09

ropable