cycurl.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import pycurl
  2. from io import BytesIO
  3. from urllib.parse import urlencode
  4. class cyConn():
  5. buf = ''
  6. c = pycurl.Curl()
  7. url = ''
  8. def __init__(self, endpoint):
  9. self.endpoint = endpoint
  10. def cycurl(self, method, param=None):
  11. self.buf = BytesIO()
  12. if param is None:
  13. self.url = "{}{}".format(self.endpoint, method)
  14. else:
  15. self.url = "{}{}?{}".format(self.endpoint, method, param)
  16. self.c.setopt(self.c.URL, self.url)
  17. self.c.setopt(self.c.WRITEFUNCTION, self.buf.write)
  18. return self.c
  19. def post(self, method, param):
  20. param = urlencode(param)
  21. self.cycurl(method)
  22. self.c.setopt(self.c.POSTFIELDS, param)
  23. self.c.perform()
  24. return self.buf.getvalue().decode('utf-8')
  25. def get(self, method, param):
  26. if param is not None:
  27. param = urlencode(param)
  28. self.cycurl(method, param)
  29. self.c.perform()
  30. return self.buf.getvalue().decode('UTF-8')