def send_post_request(http_object): # HTTPObject.convert_to_string(http_object) # print request object socket_object = socket.socket() # create socket object try: socket_object.connect( (HTTPObject.get_host(http_object), HTTPObject.get_port(http_object))) # connect to server socket_object.settimeout(2) # set timeout to 2sec if socket.gethostbyaddr(socket_object.getpeername() [0])[0]: # check remote server name print("Connected to server.....\n") except socket.error as msg: # handle socket errors print("Cannot connect to server. Try again!!!") return message = "POST " + str( HTTPObject.get_path(http_object)) + " HTTP/1.1\r\n" message += "Host: " + str(HTTPObject.get_host(http_object)) + "\r\n" for keys in HTTPObject.get_headers( http_object): # iterate over headers stored in dictionary message += keys + ": " + HTTPObject.get_headers( http_object)[keys] + "\r\n" if HTTPObject.get_inline( http_object) == "true": # check if data is provided as inline if "Content-Type" not in HTTPObject.get_headers(http_object): message += "Content-Type: text/plain" + "\r\n" message += "Content-Length: " + str( len(HTTPObject.get_data(http_object))) + "\r\n\r\n" message += str(HTTPObject.get_data(http_object)) elif HTTPObject.get_read_file( http_object ) == "true": # check if data is to be read from the file if "Content-Type" not in HTTPObject.get_headers(http_object): if str(HTTPObject.get_file1(http_object)).endswith(".txt"): message += "Content-Type: text/plain" + "\r\n" elif str(HTTPObject.get_file1(http_object)).endswith(".html"): message += "Content-Type: text/html" + "\r\n" elif str(HTTPObject.get_file1(http_object)).endswith(".xml"): message += "Content-Type: text/xml" + "\r\n" elif str(HTTPObject.get_file1(http_object)).endswith(".json"): message += "Content-Type: application/json" + "\r\n" else: message += "Content-Type: text/plain" + "\r\n" message += "Content-Length: " + str(len(HTTPConnection.read_from_file(HTTPObject.get_file1(http_object)))) \ + "\r\n\r\n" message += HTTPConnection.read_from_file( HTTPObject.get_file1(http_object)) print("Sending message:\n") print(message) socket_object.sendall(bytes(message, 'utf-8')) # send message to remote server HTTPConnection.receive_response( http_object, socket_object) # receive response from the server
def send_post_request(self): # HTTPObject.convert_to_string(self.http_object) # print request object message = "POST " + str(HTTPObject.get_path( self.http_object)) + " HTTP/1.1\r\n" message += "Host: " + str(HTTPObject.get_host( self.http_object)) + "\r\n" for keys in HTTPObject.get_headers( self.http_object): # iterate over headers stored in dictionary message += keys + ": " + HTTPObject.get_headers( self.http_object)[keys] + "\r\n" if HTTPObject.get_inline( self.http_object ) == "true": # check if data is provided as inline if "Content-Type" not in HTTPObject.get_headers(self.http_object): message += "Content-Type: text/plain" + "\r\n" message += "Content-Length: " + str( len(HTTPObject.get_data(self.http_object))) + "\r\n\r\n" message += str(HTTPObject.get_data(self.http_object)) elif HTTPObject.get_read_file( self.http_object ) == "true": # check if data is to be read from the file if "Content-Type" not in HTTPObject.get_headers(self.http_object): if str(HTTPObject.get_file1( self.http_object)).endswith(".txt"): message += "Content-Type: text/plain" + "\r\n" elif str(HTTPObject.get_file1( self.http_object)).endswith(".html"): message += "Content-Type: text/html" + "\r\n" elif str(HTTPObject.get_file1( self.http_object)).endswith(".xml"): message += "Content-Type: text/xml" + "\r\n" elif str(HTTPObject.get_file1( self.http_object)).endswith(".json"): message += "Content-Type: application/json" + "\r\n" else: message += "Content-Type: text/plain" + "\r\n" message += "Content-Length: " + str( len(HTTPConnection.read_from_file(HTTPObject.get_file1(self.http_object)))) \ + "\r\n\r\n" message += HTTPConnection.read_from_file( HTTPObject.get_file1(self.http_object)) print("Sending message:\n") print(message) self.communicate_with_server(message.encode('utf-8')) print("Data Send Complete")