Python 3, http.client HTTP памылка 400 (Python 3, http.client HTTP error 400)


問題描述

Python 3, http.client HTTP памылка 400 (Python 3, http.client HTTP error 400)

I've been working on a small pet project where I am using http.client to communicate (sorry if that's bad terminology, feel free to correct me) with omdbapi. From here I access the data on the website using the following code:

import http.client, json
__connection = http.client.HTTPConnection("www.omdbapi.com")

def getDetailsFromTitle(title):
    __connection.request("GET", "/?t=" + title)
    return __processRequest()

def getDetailsFromID(id):
     __connection.request("GET", "/?i=" + id)
     return __processRequest()

def __processRequest():
    try:
        response = __connection.getresponse()
        data = response.read()
        data = data.decode("utf‑8")
        return json.loads(data)
except: return None

This worked fine for my first few trials, like I was able to get all my data back properly if I looked up say "Eureka", or "Superbad"; however, as soon as I inputted "Bad Kids Go To Hell" my try in __processRequest was being broken and None was being returned.  The data I managed to get from the request was:

<!DOCTYPE HTML PUBLIC "‑//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP‑EQUIV="Content‑Type" Content="text/html; charset=us‑ascii"></HEAD>
<BODY><h2>Bad Request</h2>
<hr><p>HTTP Error 400. The request is badly formed.</p>
</BODY></HTML>  

It's obviously breaking on the json.loads(data) as data isn't what is expected, but I'm not overly sure why I'm receiving this error. I went to the website and inputted "Bad Kids Go To Hell" and all worked fine. 

Please let me know if you need anything more to assist me, Thank you.

‑‑‑‑‑

參考解法

方法 1:

To create the parameter lists of your urls, you should use [urlencode()][1]. In this case it's likely the spaces in the string that causes the problem, they should be converted to '+' characters.

__connection.request("GET", "/"+(urlencode({'t': title})

(by HealsgoodLennart Regebro)

參考文件

  1. Python 3, http.client HTTP error 400 (CC BY‑SA 3.0/4.0)

#HTTP #imdb #python-3.x






相關問題

POST 和 PUT HTTP 請求有什麼區別? (What's the difference between a POST and a PUT HTTP REQUEST?)

發出原始 HTTP 請求時如何輕鬆解碼 HTTP 分塊編碼字符串? (How to easily decode HTTP-chunked encoded string when making raw HTTP request?)

如何通過 HttpRequest 從連接的 Android 設備訪問本地 tomcat (How to access local tomcat via HttpRequest from connected Android device)

Python 3, http.client HTTP памылка 400 (Python 3, http.client HTTP error 400)

Tập lệnh PHP có thể tiếp tục sau khi chuyển hướng không? (Is it possible for PHP script to continue after redirect?)

兩個進程使用同一個端口? (Two processes using the same port?)

Javascript HTTP 函數 (Javascript HTTP Function)

如何通過powershell獲取請求的authtoken (How to obtain authtoken for request via powershell)

我在哪裡將 REST 客戶端身份驗證數據放在查詢中? (Where do I put the REST Client Authentication Data in the Query?)

jquery get / post請求的Apache url替換 (Apache url replacement for jquery get / post request)

在 MATLAB 中通過 HTTP 接收數據 (Receive data via HTTP in MATLAB)

在 python 中開發時,如何在 post 請求中使用“format=json&data=”? (How do I use "format=json&data=" in post requests when developing in python?)







留言討論