Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python请求对POST数据进行编码_Python_Python 2.7_Python Requests - Fatal编程技术网

Python请求对POST数据进行编码

Python请求对POST数据进行编码,python,python-2.7,python-requests,Python,Python 2.7,Python Requests,版本:Python 2.7.3 其他库:Python请求1.2.3、jinja2(2.6) 我有一个向论坛提交数据的脚本,问题是非ascii字符显示为垃圾。例如,像安德烈·特切尼这样的名字被称为安德烈·特切尼 以下是提交数据的方式: 1) 数据最初从UTF-8编码的CSV文件加载,如下所示: entries = [] with codecs.open(filename, 'r', 'utf-8') as f: for row in unicode_csv_reader(f.readlin

版本:Python 2.7.3

其他库:Python请求1.2.3、jinja2(2.6)

我有一个向论坛提交数据的脚本,问题是非ascii字符显示为垃圾。例如,像安德烈·特切尼这样的名字被称为安德烈·特切尼

以下是提交数据的方式:

1) 数据最初从UTF-8编码的CSV文件加载,如下所示:

entries = []
with codecs.open(filename, 'r', 'utf-8') as f:
    for row in unicode_csv_reader(f.readlines()[1:]):
        entries.append(dict(zip(csv_header, row)))
unicode_csv_阅读器位于Python csv文档页面的底部:

在解释器中键入条目名称时,我看到名称为
u'Andr\xe9 T\xe9chin\xe9'

2) 接下来,我通过jinja2渲染数据:

tpl = tpl_env.get_template(u'forumpost.html')
rendered = tpl.render(entries=entries)
当我在解释器中输入呈现的名称时,我再次看到相同的名称:
u'Andr\xe9 T\xe9chin\xe9'

现在,如果我将渲染变量写入如下文件名,它将正确显示:

with codecs.open('out.txt', 'a', 'utf-8') as f:
    f.write(rendered)
但我必须将其发送至论坛:

3) 在POST请求代码中,我有:

params = {u'post': rendered}
headers = {u'content-type': u'application/x-www-form-urlencoded'}
session.post(posturl, data=params, headers=headers, cookies=session.cookies)
会话是请求会话

这个名字会在论坛帖子中显示出来。我尝试了以下方法:

'Andr\xc3\xa9 T\xc3\xa9chin\xc3\xa9'
  • 省去标题
  • 编码呈现为呈现。编码('utf-8')(相同结果)
  • rendered=urllib.quote_plus(rendered)(显示为全部%XY)
如果我键入rendered.encode('utf-8'),我会看到以下内容:

'Andr\xc3\xa9 T\xc3\xa9chin\xc3\xa9'

我如何解决这个问题?谢谢。

尝试解码为utf8:

unicode(my_string_variable, "utf8")
或解码和编码:

sometext = gettextfromsomewhere().decode('utf-8')
env = jinja2.Environment(loader=jinja2.PackageLoader('jinjaapplication', 'templates'))
template = env.get_template('mypage.html')
print template.render( sometext = sometext ).encode('utf-8')

您的客户端按其应有的方式运行,例如作为服务器运行
nc-l 8888
,并发出请求:

import requests

requests.post('http://localhost:8888', data={u'post': u'Andr\xe9 T\xe9chin\xe9'})
显示:

POST / HTTP/1.1
Host: localhost:8888
Content-Length: 33
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate, compress
Accept: */*
User-Agent: python-requests/1.2.3 CPython/2.7.3

post=Andr%C3%A9+T%C3%A9chin%C3%A9
您可以检查它是否正确:

>>> import urllib
>>> urllib.unquote_plus(b"Andr%C3%A9+T%C3%A9chin%C3%A9").decode('utf-8')
u'Andr\xe9 T\xe9chin\xe9'
  • 检查服务器是否正确解码请求。您可以尝试指定字符集:

    headers = {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}
    
    正文仅包含ascii字符,因此不会造成伤害,正确的服务器将忽略
    x-www-form-urlencoded
    类型的任何参数。在中查找血淋淋的细节


  • 检查问题是否为显示伪影,即值正确但显示不正确


“检查问题是否为显示伪品,即值是否正确,但显示不正确”-谢谢。这就是问题所在!不幸的是,这是一个公共论坛,我无法更改默认编码。它以iso-8859-1编码响应。我可以使用rendered.encode('iso-8859-1')吗?或者这会破坏东西吗?谢谢。试着在标题中设置字符集将其作为渲染发送。编码('iso-8859-1')似乎可以工作,所以我将使用它。我认为你的答案是正确的,因为它指向了正确的方向。谢谢。对于其他找到此文件的人,您可以使用
urllib.parse.quote_from_bytes
urllib.parse.unquote_To_bytes
通过网络发送字节类型,而不必担心编码问题。@MicahSmith:问题有标记。那里没有
urllib.parse
。无论如何,输入是Unicode(正如它应该的那样——使用Unicode来表示程序中的文本)。旁注:
unquote\u plus()
用于方便操作
requests.post()
正常工作——在实际代码中不使用它。