Skip to main content

Like Requests, but using urllib2.

Project description

A Python wrapper for the built-in urllib2 module. The API is compatible with the excellent Requests library, but omitting features such as sessions and support for keep-alive.

Notrequests is intended for doing HTTP requests on Google App Engine where Requests has some disadvantages.

It is not Python 3 compatible (yet).

Installation

From PyPI:

$ pip install notrequests

Or download and run setup as normal:

$ curl -L -o notrequests.zip https://github.com/davidwtbuxton/notrequests/archive/master.zip
$ unzip notrequests.zip
$ cd notrequests-master
$ python setup.py install

Usage

Notrequests is compatible with the Requests API (or it tries to be).

>>> import notrequests
>>>
>>> response = notrequests.get('http://httpbin.org/get')
>>> response.status_code == notrequests.codes.ok
True

But it doesn’t do everything that Requests does. There’s no session support, no keep-alive support and it reads the entire response into memory.

Notrequests uses urllib2 but behaves more like Requests. So it won’t throw an exception on 4xx and 5xx responses.

>>> response = notrequests.get('http://httpbin.org/status/404')
>>> response.status_code == notrequests.codes.not_found
True

You can do basic auth just like Requests (but not other authentication types):

>>> url = 'http://httpbin.org/basic-auth/alice/secret'
>>> response = notrequests.get(url)
>>> response.status_code
401
>>> response = notrequests.get(url, auth=('alice', 'secret'))
>>> response.status_code
200

And send and decode JSON:

>>> import pprint
>>> response = notrequests.put('http://httpbin.org/put', json={'foo': ['bar', 'baz']})
>>> data = response.json()
>>> pprint.pprint(data)
{u'args': {},
 u'data': u'{"foo": ["bar", "baz"]}',
 u'files': {},
 u'form': {},
 u'headers': {u'Accept-Encoding': u'identity',
              u'Content-Length': u'23',
              u'Content-Type': u'application/json',
              u'Host': u'httpbin.org',
              u'User-Agent': u'notrequests/0.1'},
 u'json': {u'foo': [u'bar', u'baz']},
 u'origin': u'10.10.10.1',
 u'url': u'http://httpbin.org/put'}

There’s also support for uploading files:

>>> import io
>>> fileobj = io.BytesIO('foo bar baz')
>>> response = notrequests.post('http://httpbin.org/post', files={'upload': fileobj})
>>> response.json()['files']
{u'upload': 'foo bar baz'}

As with Requests, the keys in the files dict are the form field input names and the values in the files dict can be a 2-tuple of file name with file object or byte string:

>>> files = {'upload': ('my-file.txt', b'Foo\nbar\nbaz.')}
>>> response = notrequests.post('http://httpbin.org/post', files=files)
>>> print response.request.data
--10.10.10.1.503.2717.1443987498.810.2
Content-Disposition: file; name="upload"; filename="my-file.txt"
Content-Type: text/plain

Foo
bar
baz.
--10.10.10.1.503.2717.1443987498.810.2--

API compatibility

These are some features of the Requests API that Notrequests hasn’t implemented. It isn’t a complete list, and it would be nice to have better support.

  • Sessions

  • Request.text and auto-detection of encodings

  • Response.history

  • Response.raise_for_status()

  • Response.links

  • Streaming uploads / downloads and iterating over data

  • Alternate names for status codes

  • Proxies

Tests

Run the tests with tox.

By default the tests make requests to http://httpbin.org, but you can run a local instance which will speed things up.

$ pip install httpbin gunicorn
$ gunicorn --bind 127.0.0.1:8888 httpbin:app
$ export NOTREQUESTS_TEST_URL="http://127.0.0.1:8888"
$ tox

Why not use Requests?

Google App Engine patches httplib in the standard library to use its urlfetch service, and restricts the sockets API to paid applications. Requests does not use httplib and uses sockets.

If you want to use the app identity service to authenticate connections between App Engine applications you have to use the urlfetch service, you cannot use Requests. Notrequests works because it uses urllib2, which uses httplib.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

notrequests-0.2.1.tar.gz (6.6 kB view details)

Uploaded Source

File details

Details for the file notrequests-0.2.1.tar.gz.

File metadata

  • Download URL: notrequests-0.2.1.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for notrequests-0.2.1.tar.gz
Algorithm Hash digest
SHA256 cbd3bb1a52a7493fe5ae59ccc32cf21007c188b5e07be581a8e5883c0da9d414
MD5 5055e8550f4f5d8827d3f52d4a9478df
BLAKE2b-256 0fc6a306070656fa6e07313c348cba439653e9b1e1289579003f25ccb9864585

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page