123456789101112131415161718192021222324252627282930313233343536373839404142 |
- import pycurl
- from io import BytesIO
- from urllib.parse import urlencode
- class cyConn():
-
- buf = ''
- c = pycurl.Curl()
- url = ''
- def __init__(self, endpoint):
- self.endpoint = endpoint
-
- def cycurl(self, method, param=None):
- self.buf = BytesIO()
- if param is None:
- self.url = "{}{}".format(self.endpoint, method)
- else:
- self.url = "{}{}?{}".format(self.endpoint, method, param)
- self.c.setopt(self.c.URL, self.url)
- self.c.setopt(self.c.WRITEFUNCTION, self.buf.write)
- return self.c
- def post(self, method, param):
- param = urlencode(param)
- self.cycurl(method)
- self.c.setopt(self.c.POSTFIELDS, param)
- self.c.perform()
- return self.buf.getvalue().decode('utf-8')
- def get(self, method, param):
- if param is not None:
- param = urlencode(param)
- self.cycurl(method, param)
- self.c.perform()
- return self.buf.getvalue().decode('UTF-8')
|