fork download
  1. import httplib
  2. import urllib2
  3. import time
  4. import requests
  5.  
  6. HTTPLIB_CON = httplib.HTTPConnection('2ch.hk')
  7. REQUESTS_SESSION = requests.Session()
  8. TIMES = 1000
  9.  
  10.  
  11. def timeit(times):
  12. def decorator(func):
  13.  
  14. def _f(*args, **kwargs):
  15. start = time.time()
  16. for _ in range(times):
  17. func(*args, **kwargs)
  18. print('finished in %s' % (time.time() - start))
  19. return _f
  20.  
  21. return decorator
  22.  
  23.  
  24. @timeit(TIMES)
  25. def httplib_test():
  26. HTTPLIB_CON.request('GET', '/b/', headers={'Connection': 'keep-alive'})
  27. HTTPLIB_CON.getresponse().read()
  28.  
  29.  
  30. @timeit(TIMES)
  31. def urllib_test():
  32. urllib2.urlopen('http://2...content-available-to-author-only...h.hk/b/').read()
  33.  
  34.  
  35. @timeit(TIMES)
  36. def requests_test():
  37. r = REQUESTS_SESSION.get('http://2...content-available-to-author-only...h.hk/b/')
  38. r.text
  39.  
  40.  
  41. print('requests')
  42. requests_test()
  43.  
  44. print('HTTPlib')
  45. httplib_test()
  46.  
  47. print('urllib')
  48. urllib_test()
  49.  
Runtime error #stdin #stdout #stderr 1.36s 23168KB
stdin
Standard input is empty
stdout
requests
stderr
sh: 1: cannot create /dev/null: Permission denied
sh: 1: cannot create /dev/null: Permission denied
sh: 1: cannot create /dev/null: Permission denied
sh: 1: cannot create /dev/null: Permission denied
sh: 1: cannot create /dev/null: Permission denied
sh: 1: cannot create /dev/null: Permission denied
sh: 1: cannot create /dev/null: Permission denied
Traceback (most recent call last):
  File "prog.py", line 42, in <module>
  File "prog.py", line 17, in _f
  File "prog.py", line 37, in requests_test
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 477, in get
    return self.request('GET', url, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 465, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 573, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 415, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', gaierror(-2, 'Name or service not known'))