📜 ⬆️ ⬇️

Library to simplify HTTP requests

Requests is a Python library that elegantly and simply performs HTTP requests. Now you do not need to master urllib2 with unnecessarily complex program interfaces.

Here is the HTTP request with authorization using requests:

>>> r = requests.get('https://api.github.com', auth=('user', 'pass')) >>> r.status_code 200 >>> r.headers['content-type'] 'application/json' 

For comparison, here is the HTTP request urllib2:
')
 #!/usr/bin/env python # -*- coding: utf-8 -*- import urllib2 gh_url = 'https://api.github.com' gh_user= 'user' gh_pass = 'pass' req = urllib2.Request(gh_url) password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm() password_manager.add_password(None, gh_url, gh_user, gh_pass) auth_manager = urllib2.HTTPBasicAuthHandler(password_manager) opener = urllib2.build_opener(auth_manager) urllib2.install_opener(opener) handler = urllib2.urlopen(req) print handler.getcode() print handler.headers.getheader('content-type') # ------ # 200 # 'application/json' 

The requests library allows you to send HTTP requests for HEAD, GET, POST, PUT, PATCH and DELETE. All headers and parameters are added very simply, as well as processing server responses. Of course, requests works on the basis of urllib2, but it does all the hard work.

Source code requests on github , under an open ISC license
Documentation
Requests API

Source: https://habr.com/ru/post/126262/


All Articles