开发者

Python error: TypeError: POST data should be bytes

In my below code I am getting an error "raise Ty开发者_如何转开发peError("POST data should be bytes" TypeError: POST data should be bytes or an iterable of bytes. It cannot be str."

What am I doing wrong ? I am using python 3.2.2

Below is the code:

msg = "Test post"
password_manager = urllib.request.HTTPPasswordMgr()
password_manager.add_password("Twitter API",
"http://twitter.com/statuses", "sampleusername", "password")
http_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
page_opener = urllib.request.build_opener(http_handler)
urllib.request.install_opener(page_opener)
params = urllib.parse.urlencode( {'status':msg} )
resp = urllib.request.urlopen("http://twitter.com/statuses/update.json", params)
resp.read()


It means what it says - in Python 3, strings are unicode by default, but you can't post unicode: you have to use a bytestring.

This should work:

msg = b"Test post"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜