如何在urllib2请求中发送utf-8内容? [英] How to send utf-8 content in a urllib2 request?

查看:563
本文介绍了如何在urllib2请求中发送utf-8内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在过去半天遇到以下问题,尽管我已经找到了一些有关类似问题的信息,但是没有什么真正的命中。



我正在尝试使用urllib2发送一个PUT请求,其中包含一些Unicode字符:

  body = u'{bbb :asdf\xd7\xa9\xd7\x93\xd7\x92}'
conn = urllib2.Request(request_url,body,headers)
conn.get_method = lambda :'PUT'
response = urllib2.urlopen(conn)

使用 body = body.encode('utf-8')和其他变体,但无论如何,我会收到以下错误:

  UnicodeEncodeError at ... 
'ascii'编解码器无法解码位置15中的字节0xc3:序号不在范围(128)

使用以下调用堆栈之一:

 文件...in ... 
195. response = urllib2.urlopen( conn)
文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py在urlopen
126. return _opener.open(url,data,超时)
文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py在open
394. response = self._open(req,data )
文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py在_open
412.'_open',req)
文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.pyin _call_chain
372. result = func(* args)
文件/系统/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py在http_open
1199. return self.do_open(httplib.HTTPConnection,req)
文件/系统do_open
1168中的/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py。h.request(req.get_method(),req.get_selector(),re q.data,headers)
文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py中的请求
955. self._send_request(method ,url,body,headers)
文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py在_send_request
989. self.endheaders( body)
文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.pyin endheaders
951. self._send_output(message_body)
文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py在_send_output
815. self.send(message_body)
文件/系统/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.pyin send
787. self.sock.sendall(data)
文件/ System / Library /框架/ Python.framework /版本/ 2.7 / lib / python2.7 / socket.pyin meth
224. return getattr(self._sock,name)(* args)

或者以下调用堆栈(当我做 body = body.encode('utf-8')):

 文件...in ... 
195. response = urllib2.urlopen(conn)
文件/ System / Library / Frameworks / Python。框架/版本/ 2.7 / lib / python2.7 / urllib2.py在urlopen
126. return _opener.open(url,data,timeout)
文件/ System / Library / Frameworks / Python。框架/版本/ 2.7 / lib / python2.7 / urllib2.py打开
394. response = self._open(req,data)
文件/System/Library/Frameworks/Python.framework _Venions/2.7/lib/python2.7/urllib2.py在_open
412.'_open',req)
文件/System/Library/Frameworks/Python.framework/Versions/2.7/ lib / python2.7 / urllib2.pyin _call_chain
372. result = func(* args)
文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2。 7 / urllib2.pyin http_open
1199. return self.do_open(httplib.HTTPConnection,req )
文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.pyin do_open
1168. h.request(req.get_method(), req.get_selector(),req.data,headers)
文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py中的请求
955 。self._send_request(method,url,body,headers)
文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py在_send_request
989. self.endheaders(body)
文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.pyin endheaders
951. self._send_output (message_body)
文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py在_send_output
809. msg + = message_body

我做错了什么?如何通过urllib2发送带有Unicode字符的正文?如果没有Unicode字符,一切都正常。



另请注意,我的内容类型标题设置为



如果它以任何方式相关,我的内容是什么'做的是这样的:我要求我的Django服务器,我将请求委托给另一个Django服务器。我不重定向,只需从我自己的服务器发送请求获取响应并发回它。所以 body 是Django视图中的 request.body



编辑



我的标题是:

 code> {
'Origin':'http://10.0.0.146:8000',
'Accept-Language':'en-US,en; q = 0.8',
'Accept-Encoding':'gzip,deflate,sdch',
'Host':'localhost:5000',
'Accept':'application / json,text / plain,* / * ',
'User-Agent':'Mozilla / 5.0(Macintosh; Intel Mac OS X 10_8_3)AppleWebKit / 537.31(KHTML,像Gecko)Chrome / 26.0.1410.65 Safari / 537.31',
'接受-Charset':'ISO-8859-1,utf-8; q = 0.7,*; q = 0.3',
'Connection':'keep-alive',
'X-Requested-With ':'XMLHttpRequest',
'Pragma':'no-cache',
'Cache-Control':'no-cache',
'Referer':'http:// localhost :5000 /',
'Content-Type':'application / json; charset = UTF-8',
'Authorization':'ApiKey ogkLPgSESNyTOgIdbSLDhJjvy VJcbg:0d5897b5204c2f2527f532c6a97ba18a7f06acdc',
'Cookie':'username = ogkLPgSESNyTOgIdbSLDhJjvyVJcbg; _we_wk_ls _ =%7B%22time%22%3A1369123506709%7D; __jwpusr = 39e63770-ec5c-4b96-9f7f-b199703d0d36;的sessionid = 0d741a7560258b301979a1c853b42a81; api_key = 0d5897b5204c2f2527f532c6a97ba18a7f06acdc'
}


解决方案

只传递字节串到请求。如果这三个输入中的任何一个包含Unicode值,则Unicode和字符串之间的自动转换将在连接时发生,其中将永远导致悲伤。


I'm struggling with the following question for the past half a day and although I've found some info about similar problems, nothing really hits the spot.

I'm trying to send a PUT request using urllib2 with data that contains some Unicode characters:

body = u'{ "bbb" : "asdf\xd7\xa9\xd7\x93\xd7\x92"}'
conn = urllib2.Request(request_url, body, headers)
conn.get_method = lambda: 'PUT'
response = urllib2.urlopen(conn)

I've tried to use body = body.encode('utf-8') and other variations, but whatever I do I get the following error:

UnicodeEncodeError at ...
'ascii' codec can't decode byte 0xc3 in position 15: ordinal not in range(128)

With one of the following call stacks:

File "..." in ...
  195.         response = urllib2.urlopen(conn)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py" in urlopen
  126.     return _opener.open(url, data, timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py" in open
  394.         response = self._open(req, data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py" in _open
  412.                                   '_open', req)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py" in _call_chain
  372.             result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py" in http_open
  1199.         return self.do_open(httplib.HTTPConnection, req)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py" in do_open
  1168.             h.request(req.get_method(), req.get_selector(), req.data, headers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py" in request
  955.         self._send_request(method, url, body, headers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py" in _send_request
  989.         self.endheaders(body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py" in endheaders
  951.         self._send_output(message_body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py" in _send_output
  815.             self.send(message_body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py" in send
  787.             self.sock.sendall(data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py" in meth
  224.     return getattr(self._sock,name)(*args)

Or the following call stack (for when I do body = body.encode('utf-8')):

File "..." in ...
  195.         response = urllib2.urlopen(conn)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py" in urlopen
  126.     return _opener.open(url, data, timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py" in open
  394.         response = self._open(req, data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py" in _open
  412.                                   '_open', req)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py" in _call_chain
  372.             result = func(*args)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py" in http_open
  1199.         return self.do_open(httplib.HTTPConnection, req)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py" in do_open
  1168.             h.request(req.get_method(), req.get_selector(), req.data, headers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py" in request
  955.         self._send_request(method, url, body, headers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py" in _send_request
  989.         self.endheaders(body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py" in endheaders
  951.         self._send_output(message_body)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py" in _send_output
  809.             msg += message_body

What am I doing wrong? How can I send a body with Unicode characters via urllib2? If there are no Unicode characters, everything works fine.

Also note that my Content-Type header is set to application/json;charset=utf-8.

If it's relevant in any way, the context of what I'm doing is this: I'm getting a request to my Django server, and I delegate the request to another Django server. I don't redirect, just send the request from my own server get the response and send it back. So body is the request.body in the Django view.

Edit:

My headers are:

{
'Origin': 'http://10.0.0.146:8000', 
'Accept-Language': 'en-US,en;q=0.8', 
'Accept-Encoding': 'gzip,deflate,sdch', 
'Host': 'localhost:5000', 
'Accept': 'application/json, text/plain, */*', 
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31', 
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 
'Connection': 'keep-alive', 
'X-Requested-With': 'XMLHttpRequest', 
'Pragma': 'no-cache', 
'Cache-Control': 'no-cache', 
'Referer': 'http://localhost:5000/', 
'Content-Type': 'application/json;charset=UTF-8', 
'Authorization': 'ApiKey ogkLPgSESNyTOgIdbSLDhJjvyVJcbg:0d5897b5204c2f2527f532c6a97ba18a7f06acdc', 
'Cookie': 'username=ogkLPgSESNyTOgIdbSLDhJjvyVJcbg; _we_wk_ls_=%7B%22time%22%3A1369123506709%7D; __jwpusr=39e63770-ec5c-4b96-9f7f-b199703d0d36; sessionid=0d741a7560258b301979a1c853b42a81; api_key=0d5897b5204c2f2527f532c6a97ba18a7f06acdc'
}

解决方案

You need to pass only byte strings to Request. This applies to the headers, the url and the body.

If any of those three inputs contain Unicode values, automatic conversions between Unicode and strings will take place when concatenating, which will invariably lead to grief.

这篇关于如何在urllib2请求中发送utf-8内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆