Skip to content

Instantly share code, notes, and snippets.

@fawkesley
Last active May 30, 2018 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fawkesley/7a83bac60707a618cd896cdda8823c36 to your computer and use it in GitHub Desktop.
Save fawkesley/7a83bac60707a618cd896cdda8823c36 to your computer and use it in GitHub Desktop.
Wrapper for python requests providing session and user agent
import requests
class RequestsWrapper():
def __init__(self, user_agent):
self._session = requests.Session()
self._user_agent = user_agent
def get(self, *args, **kwargs):
headers = requests.structures.CaseInsensitiveDict(
kwargs.get('headers', {})
)
if 'user-agent' not in headers:
headers['user-agent'] = self._user_agent
timeout = kwargs.pop('timeout', 10)
return self._session.get(headers=headers, timeout=timeout, *args, **kwargs)
@fawkesley
Copy link
Author

> curl -L https://gist.github.com/paulfurley/7a83bac60707a618cd896cdda8823c36/raw/ef96fdd03fe35ccd5666bc43aa7724a96900bb4c/requests_wrapper.py > requests_wrapper.py`

Then in your code:

from requests_wrapper import RequestsWrapper

http = RequestsWrapper('bot: github.com/myusername/myproject contact@mydomain.com')
http.get('http://www.example.com')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment