fork download
  1. # -*- coding: utf-8 -*-
  2.  
  3. from urllib2 import Request, urlopen, URLError
  4. import codecs
  5.  
  6. FIREFOX_UBUNTU_UAS = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:23.0) Gecko/20100101 Firefox/23.0'
  7.  
  8. def GetHttpSource(url, cookies='', user_agent=FIREFOX_UBUNTU_UAS):
  9. req = Request(url)
  10. req.add_header('User-Agent', user_agent)
  11. req.add_header('Cookie', cookies)
  12. try:
  13. response = urlopen(req)
  14. except URLError, e:
  15. if hasattr(e, 'reason'):
  16. print 'We failed to reach a server.'
  17. print 'Reason: ', e.reason
  18. elif hasattr(e, 'code'):
  19. print 'The server couldn\'t fulfill the request.'
  20. print 'Error code: ', e.code
  21. return None
  22. else:
  23. return response.read()
Success #stdin #stdout 0.12s 12712KB
stdin
Standard input is empty
stdout
Standard output is empty