示例#1
0
 def start_request(self, method="GET",data=None):
     """
     :param method:
     :param data: data to send to server.
     :return: str
     """
     raw_data = None
     try:
         self.__client.setup(post=data)
         answer = self.__client.request()
         if answer.code != 200:
             logger.warning(
                 "failed to get response from server! error: {0}\n".format(http.client.responses[answer.code]))
         raw_data = answer.unicode_body()
     except Exception as err:
         logger.log(logger.DETAIL, str(err) + " when request to {0}\n".format(
             self.__target.netloc + self.__target.path))  # when error happened, it often
         # means there is still a connection
         # thus just ignore it.
     return raw_data
示例#2
0
 def start_request(self, method="GET", url="", data=""):
     """
     :param method:
     :param url:
     :param data: data to send to server.
     :return: str
     """
     url = self.__target.path + url
     raw_data = None
     try:
         self.__client.connect()
         if method == "GET":
             data = url + data
             data = str.replace(data, r'//', r'/')
             self.__client.putrequest(method, data)
             data = ""
         else:
             self.__client.putrequest(method, url)
         data = urllib.parse.quote(data, '=&')
         self.__client.putheader('Content-Length', len(data))
         self.__put_def_header()
         self.__put_client_header()
         self.__client.endheaders()
         self.__client.send(data.encode())
         anwser = self.__client.getresponse()
         if anwser.status != 200:
             logger.warning(
                 "failed to get response from server! error: {0}".format(http.client.responses[anwser.status]))
         raw_data = str(anwser.read().decode())
     except Exception as err:
         logger.log(logger.DETAIL, str(err) + " when request to {0}".format(
             self.__target.netloc + self.__target.path))  # when error happened, it often
         # means there is still a connection
         # thus just ignore it.
     self.__client.close()
     return raw_data