Mosa Abbas
Mosa Abbas

Reputation: 149

why i keep having the same error in that code

i'm following a tutorial course i'm trying to make a web application in python 3.7 that see if there is any bad words in a text file

the text file contains the following text :

-- Houston, we have a problem. (Apollo 13)

-- Mama always said, life is like a box of chocolates. You never know what you are going to get. (Forrest Gump)

-- You cant handle the truth. (A Few Good Men)

-- I believe everything and I believe nothing. (A Shot in the Dark)

import urllib.request

def read_text():
    quotes = open (r'C:\Users\M\Desktop\TEMP FILES\movie_quotes.txt')
    content_of_file = quotes.read()
    print(content_of_file)
    quotes.close()
    check_profanity(content_of_file)

def check_profanity(text):

    connection = urllib.request.urlopen( 'http://www.wdylike.appspot.com/?q='+text)
   output = connection.read()
   print(output)
   connection.close()

read_text()

and i have that error :

Traceback (most recent call last):
File "C:\Users\Mosa Abbas\Desktop\TEMP FILES\ipnd-starter-code-master\ipnd-starter-code-master\stage_3\lesson_3.3_classes\c_profanity_editor\check_profanity.py", line 32, in <module>
read_text()
File "C:\Users\Mosa Abbas\Desktop\TEMP FILES\ipnd-starter-code-master\ipnd-starter-code-master\stage_3\lesson_3.3_classes\c_profanity_editor\check_profanity.py", line 23, in read_text
check_profanity(content_of_file)
File "C:\Users\Mosa Abbas\Desktop\TEMP FILES\ipnd-starter-code-master\ipnd-starter-code-master\stage_3\lesson_3.3_classes\c_profanity_editor\check_profanity.py", line 27, in check_profanity
connection = urllib.request.urlopen( 'http://www.wdylike.appspot.com/?q='+text)
File "C:\Users\Mosa Abbas\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\Mosa Abbas\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Users\Mosa Abbas\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\Mosa Abbas\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "C:\Users\Mosa Abbas\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "C:\Users\Mosa Abbas\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request

what is that error and what i should do to get rid of it

urllib.error.HTTPError: HTTP Error 400: Bad Request

i'v tried to add this line of code :

url = 'http://www.wdylike.appspot.com/?q='+urllib.parse.quote(text, safe = '/')

and then:

connection = urllib.request.urlopen(url)

and i got

 raise RemoteDisconnected("Remote end closed connection without"
 http.client.RemoteDisconnected: Remote end closed connection without response

i'm a beginner in python and programming web applications so please help :(

Upvotes: 1

Views: 86

Answers (2)

Mosa Abbas
Mosa Abbas

Reputation: 149

i found the solution I think that happens be because i'm not encoding the string before appending it to the url.

in python3 we should do the following to 'text' before appending it to the url:

text_to_check = urllib.parse.quote_plus(text)

Python2 would be something like this

(urllib was broken into smaller components in python3):

text_to_check = urllib.quote_plus(text_to_check)

This means that, when appending a string with whitespace to the url it will appear as something like "Am+I+cursing%3F" instead of "Am I cursing?".

Full check_profanity() example:

def check_profanity(text_to_check):
    text_to_check = urllib.parse.quote_plus(text_to_check)
    connection = urlopen(
        "http://www.wdylike.appspot.com/?q=" + text_to_check)
    output = connection.read()
print(output)
connection.close()

Upvotes: 0

Carles
Carles

Reputation: 453

When sending a HTTP request through a GET method, it is necessary to escape the GET vars. So you should probably escape your text before appending it to the URL.

Have a look to question #1695183

Upvotes: 1

Related Questions