python - 如何使用 httplib 发布 unicode 字符?

标签 python unicode httplib

我尝试使用 httplib.request 函数发布 unicode 数据:

s = u"עברית"
data = """
<spellrequest textalreadyclipped="0" ignoredups="1" ignoredigits="1" ignoreallcaps="0">
<text>%s</text>
</spellrequest>
""" % s

con = httplib.HTTPSConnection("www.google.com")
con.request("POST", "/tbproxy/spell?lang=he", data)
response = con.getresponse().read()

但是这是我的错误:

Traceback (most recent call last):
  File "C:\Scripts\iQuality\test.py", line 47, in <module>
    print spellFix(u"╫á╫נ╫¿╫ץ╫ר╫ץ")
  File "C:\Scripts\iQuality\test.py", line 26, in spellFix
    con.request("POST", "/tbproxy/spell?lang=%s" % lang, data)
  File "C:\Python27\lib\httplib.py", line 955, in request
    self._send_request(method, url, body, headers)
  File "C:\Python27\lib\httplib.py", line 989, in _send_request
    self.endheaders(body)
  File "C:\Python27\lib\httplib.py", line 951, in endheaders
    self._send_output(message_body)
  File "C:\Python27\lib\httplib.py", line 815, in _send_output
    self.send(message_body)
  File "C:\Python27\lib\httplib.py", line 787, in send
    self.sock.sendall(data)
  File "C:\Python27\lib\ssl.py", line 220, in sendall
    v = self.send(data[count:])
  File "C:\Python27\lib\ssl.py", line 189, in send
    v = self._sslobj.write(data)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 97-102: or
dinal not in range(128)

我哪里错了?

最佳答案

http 没有根据特定的字符编码定义,而是使用八位字节。你需要将你的数据转换成一种编码,然后你需要告诉服务器你使用了哪种编码。让我们使用 utf8,因为它通常是最佳选择:

此数据看起来有点像 XML,但您跳过了 xml 标记。有些服务可能会接受,但无论如何你都不应该接受。事实上,编码实际上属于那里;所以一定要包含它。标题看起来像 <?xml version="1.0" encoding=" 编码 "?> .

s = u"עברית"
data_unicode = u"""<?xml version="1.0" encoding="UTF-8"?>
<spellrequest textalreadyclipped="0" ignoredups="1" ignoredigits="1" ignoreallcaps="0">
<text>%s</text>
</spellrequest>
""" % s

data_octets = data_unicode.encode('utf-8')

出于礼貌,您还应该使用content-type 告诉服务器本身格式和编码。 header :

con = httplib.HTTPSConnection("www.google.com")
con.request("POST",
            "/tbproxy/spell?lang=he", 
            data_octets, {'content-type': 'text/xml; charset=utf-8'})

编辑:它在我的机器上工作正常,你确定你没有跳过什么吗?完整示例

>>> from cgi import escape
>>> from urllib import urlencode
>>> import httplib
>>> 
>>> template = u"""<?xml version="1.0" encoding="UTF-8"?>
... <spellrequest textalreadyclipped="0" ignoredups="1" ignoredigits="1" ignoreallcaps="0">
... <text>%s</text>
... </spellrequest>
... """
>>> 
>>> def chkspell(word, lang='en'):
...     data_octets = (template % escape(word)).encode('utf-8')
...     con = httplib.HTTPSConnection("www.google.com")
...     con.request("POST",
...         "/tbproxy/spell?" + urlencode({'lang': lang}),
...         data_octets,
...         {'content-type': 'text/xml; charset=utf-8'})
...     req = con.getresponse()
...     return req.read()
... 
>>> chkspell('baseball')
'<?xml version="1.0" encoding="UTF-8"?><spellresult error="0" clipped="0" charschecked="8"></spellresult>'
>>> chkspell(corpus, 'he')
'<?xml version="1.0" encoding="UTF-8"?><spellresult error="0" clipped="0" charschecked="5"></spellresult>'

我确实注意到,当我粘贴您的示例时,它在我的终端上的显示顺序与它在我的浏览器中的显示顺序相反。考虑到希伯来语是一种从右到左的语言,这不足为奇。

>>> corpus = u"עברית"
>>> print corpus[0]
ע

关于python - 如何使用 httplib 发布 unicode 字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10149961/

相关文章:

python - 在 python 解释器中打印 unicode 字符

php - 正确处理外来字符/表情符号

python - 处理 IncompleteRead,URLError

python - Django:如何读取模型字段的 db_column 名称

python - 导入类而不执行.py 它在?

Python2 : Using . decode with errors ='replace' 仍然返回错误

python - 我想调用 HDFS REST api 来上传文件

rest - 使用 python 2.7 进行 URL 编码

python - 如何找到图像外遗漏的角落

python - 如何在Python生成的网页上显示stdout?