Esempio n. 1
0
 def receive_response(http_object, socket_object):
     # print("Inside receive response")
     _receive_data = []
     begin = time.time()  # stores current time
     while 1:
         if _receive_data and time.time() - begin > socket_object.gettimeout(
         ):  # break if time elapsed since \
             # last receive is greater than the timeout
             break
         elif time.time() - begin > socket_object.gettimeout() * 2:
             break
         try:
             _tmp = socket_object.recv(2048)  # receive 2048 bytes at a time
             if _tmp:
                 _tmp = _tmp.decode("utf-8")  # convert to text string
                 _receive_data.append(_tmp)  # append to receive array
                 begin = time.time()  # set begin to current time
         except:
             pass
     str_received_data = ''
     for data in _receive_data:
         str_received_data += data  # convert received array to string
     print("Received data:\n")
     # print(str_received_data)
     header_body = str_received_data.split(
         "\r\n\r\n")  # split header and body
     print_data = ''
     if HTTPObject.get_is_verbose(
             http_object) == "true":  # check if verbose option is enabled
         print_data += header_body[0] + "\n"
     if len(header_body
            ) == 2:  # check if body is contained in received data
         print_data += "\n" + header_body[1] + "\n"
     my_header = header_body[0].split(" ")
     if my_header[1].startswith("3") and 300 <= int(
             my_header[1]
     ) <= 304:  # check if body contains redirection code
         socket_object.close()
         loc_index = header_body[0].replace("location:", "Location:").find(
             "Location:")  # find location of new url
         start = header_body[0].find(":", loc_index) + 2
         # get start index of new url, +2 for // in http://
         end = header_body[0].find("\r\n", start)
         # get end index of new url
         --end  # move to one previous location
         HTTPObject.set_path(
             http_object, header_body[0][start:end].strip())  # set new path
         HTTPObject.set_url(http_object,
                            HTTPConnection.find_url(
                                header_body[1]))  # set new url
         HTTPConnection.send_request(
             http_object)  # send new request to redirected url
         return
     headers = {}
     count = 0
     for line in header_body[0].split("\r\n"):
         if count != 0:
             headers[line.split(":")[0].strip()] = line.split(
                 ":")[1].strip()
         else:
             count = count + 1
     if "Content-Disposition" in headers:
         if headers["Content-Disposition"].startswith("attachment"):
             pos = (headers["Content-Disposition"]).find("/")
             file = (headers["Content-Disposition"])[pos + 1:]
             if headers["Content-Type"] == "text/plain":
                 file += ".txt"
             elif headers["Content-Type"] == "text/html":
                 file += ".html"
             elif headers["Content-Type"] == "text/xml":
                 file += ".xml"
             elif headers["Content-Type"] == "application/json":
                 file += ".json"
             else:
                 file += ".txt"
             if len(header_body) > 1:
                 HTTPConnection.write_to_file(file, header_body[1])
     if HTTPObject.get_write_file(
             http_object
     ) == "true":  # check if data is to be written to a file
         HTTPConnection.write_to_file(HTTPObject.get_file2(http_object),
                                      print_data)  # write data to the file
     else:
         print(print_data)  # print data to console
Esempio n. 2
0
 def convert_response(self):
     str_received_data = ''
     for some_data in sorted(self.payload.items()):
         str_received_data += some_data[
             1]  # convert received array to string
     print("Received data:\n")
     print(str_received_data)
     header_body = str_received_data.split(
         "\r\n\r\n")  # split header and body
     print_data = ''
     if HTTPObject.get_is_verbose(
             self.http_object
     ) == "true":  # check if verbose option is enabled
         print_data += header_body[0] + "\n"
     if len(header_body
            ) == 2:  # check if body is contained in received data
         print_data += "\n" + header_body[1] + "\n"
     my_header = header_body[0].split(" ")
     if my_header[1].startswith("3") and 300 <= int(
             my_header[1]
     ) <= 304:  # check if body contains redirection code
         # socket_object.close()
         loc_index = header_body[0].replace("location:", "Location:").find(
             "Location:")  # find location of new url
         start = header_body[0].find(":", loc_index) + 2
         # get start index of new url, +2 for // in http://
         end = header_body[0].find("\r\n", start)
         # get end index of new url
         --end  # move to one previous location
         HTTPObject.set_path(
             self.http_object,
             header_body[0][start:end].strip())  # set new path
         HTTPObject.set_url(self.http_object,
                            HTTPConnection.find_url(
                                header_body[1]))  # set new url
         HTTPConnection.send_request(
             self.http_object)  # send new request to redirected url
         return
     headers = {}
     count = 0
     for line in header_body[0].split("\r\n"):
         if count != 0:
             headers[line.split(":")[0].strip()] = line.split(
                 ":")[1].strip()
         else:
             count = count + 1
     if "Content-Disposition" in headers:
         if headers["Content-Disposition"].startswith("attachment"):
             pos = (headers["Content-Disposition"]).find("/")
             file = (headers["Content-Disposition"])[pos + 1:]
             if headers["Content-Type"] == "text/plain":
                 file += ".txt"
             elif headers["Content-Type"] == "text/html":
                 file += ".html"
             elif headers["Content-Type"] == "text/xml":
                 file += ".xml"
             elif headers["Content-Type"] == "application/json":
                 file += ".json"
             else:
                 file += ".txt"
             if len(header_body) > 1:
                 HTTPConnection.write_to_file(file, header_body[1])
     if HTTPObject.get_write_file(
             self.http_object
     ) == "true":  # check if data is to be written to a file
         HTTPConnection.write_to_file(HTTPObject.get_file2(
             self.http_object), print_data)  # write data to the file
     else:
         print(print_data)  # print data to console """