pycurllib.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # A (very incomplete) urllib2-looking interface to pycurl
  2. # by: Greg Hazel
  3. import os
  4. import sys
  5. import pycurl
  6. from cStringIO import StringIO
  7. from BaseHTTPServer import BaseHTTPRequestHandler
  8. from urllib import urlencode
  9. DEBUG = False
  10. global CA_PATH
  11. CA_PATH = None
  12. http_bindaddr = None
  13. user_agent = None
  14. _pycurl_compression = 'zlib' in pycurl.version
  15. use_compression = True
  16. use_cert_authority = False
  17. timeout = 30
  18. connect_timeout = 30
  19. class StringIO2(object):
  20. def __init__(self):
  21. self.s = StringIO()
  22. def __getattr__(self, attr):
  23. return getattr(self.s, attr)
  24. def set_use_cert_authority(use):
  25. global CA_PATH
  26. global use_cert_authority
  27. use_cert_authority = use
  28. if use_cert_authority:
  29. for path in sys.path:
  30. cert = os.path.join(path, "ca-bundle.crt")
  31. if os.path.exists(cert):
  32. CA_PATH = cert
  33. break
  34. cert = os.path.join(path, "curl-ca-bundle.crt")
  35. if os.path.exists(cert):
  36. CA_PATH = cert
  37. break
  38. else:
  39. raise ImportError("Certificate Authority never found!")
  40. def set_http_bindaddr(bindaddr):
  41. global http_bindaddr
  42. http_bindaddr = bindaddr
  43. def set_user_agent(new_user_agent):
  44. global user_agent
  45. user_agent = new_user_agent
  46. def set_use_compression(use):
  47. global use_compression
  48. use_compression = use
  49. def set_timeout(t):
  50. global timeout
  51. timeout = t
  52. def set_connect_timeout(t):
  53. global connect_timeout
  54. connect_timeout = t
  55. def urlopen(req, close=True):
  56. if isinstance(req, str):
  57. req = Request(req)
  58. response = StringIO2()
  59. if DEBUG:
  60. req.c.setopt(req.c.VERBOSE, 1)
  61. req.c.setopt(req.c.WRITEFUNCTION, response.write)
  62. if req.headers:
  63. req.c.setopt(req.c.HTTPHEADER, req._make_headers())
  64. req.c.perform()
  65. response.seek(-1)
  66. #print repr(response.getvalue())
  67. response.code = req.c.getinfo(pycurl.RESPONSE_CODE)
  68. response.code = int(response.code)
  69. try:
  70. response.msg = BaseHTTPRequestHandler.responses[response.code][0]
  71. except:
  72. response.msg = "No Reason"
  73. response.content_type = req.c.getinfo(pycurl.CONTENT_TYPE)
  74. if close:
  75. req.c.close()
  76. return response
  77. class Request(object):
  78. def __init__(self, url):
  79. self.c = pycurl.Curl()
  80. self.headers = {}
  81. self.c.setopt(self.c.URL, url)
  82. if use_cert_authority:
  83. self.c.setopt(pycurl.CAINFO, CA_PATH)
  84. self.c.setopt(pycurl.CAPATH, CA_PATH)
  85. #self.c.setopt(pycurl.CA_BUNDLE, CA_PATH)
  86. else:
  87. self.c.setopt(pycurl.SSL_VERIFYHOST, 0)
  88. self.c.setopt(pycurl.SSL_VERIFYPEER, 0)
  89. if http_bindaddr:
  90. self.c.setopt(self.c.INTERFACE, http_bindaddr)
  91. if user_agent:
  92. self.c.setopt(pycurl.USERAGENT, user_agent)
  93. if use_compression:
  94. if _pycurl_compression:
  95. # If a zero-length string is set, then an Accept-Encoding header
  96. # containing all supported encodings is sent.
  97. self.c.setopt(pycurl.ENCODING, "")
  98. # someday, gzip manually with GzipFile
  99. #else:
  100. # self.add_header("Accept-Encoding", "gzip")
  101. if timeout:
  102. self.c.setopt(self.c.TIMEOUT, timeout)
  103. if connect_timeout:
  104. self.c.setopt(self.c.CONNECTTIMEOUT, timeout)
  105. def set_url(self, url):
  106. self.c.setopt(self.c.URL, url)
  107. def set_timeout(self, timeout):
  108. self.c.setopt(self.c.TIMEOUT, timeout)
  109. def set_connect_timeout(self, timeout):
  110. self.c.setopt(self.c.CONNECTTIMEOUT, timeout)
  111. def add_header(self, var, val):
  112. self.headers[var] = val
  113. def _make_headers(self):
  114. headers = []
  115. for k,v in self.headers.iteritems():
  116. headers.append(("%s: %s" % (k, v)))
  117. # turn off nasty bad evil crap
  118. headers.append("Expect:")
  119. return headers
  120. def add_data(self, data):
  121. self.data = StringIO2()
  122. self.data.write(data)
  123. self.data.seek(-1)
  124. self.c.setopt(pycurl.POST, 1)
  125. self.c.setopt(self.c.READFUNCTION, self.data.read)
  126. self.c.setopt(self.c.POSTFIELDSIZE, len(data))