Skip to content

Instantly share code, notes, and snippets.

@nashley
Created June 16, 2017 15:16
Show Gist options
  • Select an option

  • Save nashley/6f36cb251a9f30cf53c80ed7efbfa409 to your computer and use it in GitHub Desktop.

Select an option

Save nashley/6f36cb251a9f30cf53c80ed7efbfa409 to your computer and use it in GitHub Desktop.
A simple program to demonstrate how to work with grequests
#!/usr/bin/python3
#import traceback
import grequests # pip install grequests
def exception_handler(request, exception):
print("An error occured while downloading from '%s':" % request.url)
# print(dir(exception))
# print(traceback.print_tb(exception.__traceback__))
# print("\t%s" % exception)
# traceback.print_exc()
urls = [
'https://www.google.com',
'http://icanhazip.com',
'https://httpbin.org/status/404',
'https://httpbin.org/status/500',
'https://expired.badssl.com/',
'https://self-signed.badssl.com/',
'https://hsts.badssl.com/',
'https://httpbin.org/delay/60',
'not a url',
'https://cacert.org'
]
unsent_requests = []
for url in urls:
unsent_requests.append(grequests.get(
url,
timeout=5,
verify='server.cert'
)) # if you wish to verify for a self-signed cert and ignore all others
'''To get the cert for a website, use the following command:
```echo QUIT | openssl s_client -showcerts -connect cacert.org:443 | awk '/-----BEGIN CERTIFICATE-----/ {p=1}; p; /-----END CERTIFICATE-----/ {p=0}' > server.cert```'''
results = grequests.imap(unsent_requests, exception_handler=exception_handler)
for result in results:
print('WORKED: %s' % result.url)
print(result.text[:10])
# print(result.url)
# print(result.status_code)
# print('=================')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment