In Python3 how would you parse each character of an http post to ensure the requ
ID: 3833627 • Letter: I
Question
In Python3 how would you parse each character of an http post to ensure the request is valid? Primarily concerned with POST /r1.html and Content-type: ...? Possibly by import re? POST /r1.html HTTP/1.1 Host: localhost:8001 Connection: keep-alive Content-Length: 32 Cache-Control: max-age=0 Origin: http://localhost:8001 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Referer: http://localhost:8001/f1.html Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.8
Cookie: Webstorm-ea36d52b=7cf702c4-f920-40c7-acfd-a25b1d77b36c fname=John&lname=Doe&gender=male
Explanation / Answer
# open morseCode.txt file to read morseCodeFile = open('morseCode.txt', 'r') # format is :< > # create an empty list for letters letterList = [] # create an empty list for morse codes codeList = [] # read the first line of the morseCode.txt line = morseCodeFile.readline() # while the line is not empty while line != '': # strip the from the end of each line line = line.rstrip() # append the first character of the line to the letterList letterList.append(line[0]) # append the 3rd to last character of the line to the codeList codeList.append(line[2:]) # read the next line line = morseCodeFile.readline() # close the file morseCodeFile.close() try: # get user input print("Enter a string to convert to morse code or press to quit") userInput = input("") # while the user inputs something, continue while userInput: # strip the spaces from their input userInput = userInput.replace(' ', '') # convert to uppercase userInput = userInput.upper() # set string accumulator accumulateLetters = '' # go through each letter of the word for x in userInput: # get the index of the letterList using x index = letterList.index(x) # get the morse code value from the codeList using the index found above value = codeList[index] # accumulate the letter found above accumulateLetters += value # print the letters print(accumulateLetters) # input to try again or to quit print("Try again or press to quit") userInput = input("") except ValueError: print("Error in input. Only alphanumeric characters, a comma, and period allowed") main()Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.