python request.post中文失败_python3.6 request库,post文件带中文名称失败

修改Python36\Lib\site-packages\urllib3\fields.py

因为request依赖urllib

def format_header_param(name, value):

"""

Helper function to format and quote a single header parameter.

Particularly useful for header parameters which might contain

non-ASCII values, like file names. This follows RFC 2231, as

suggested by RFC 2388 Section 4.4.

:param name:

The name of the parameter, a string expected to be ASCII only.

:param value:

The value of the parameter, provided as a unicode string.

"""

if not any(ch in value for ch in '"\\\r\n'):

result = '%s="%s"' % (name, value)

try:

result.encode('ascii')

except (UnicodeEncodeError, UnicodeDecodeError):

pass

else:

return result

if not six.PY3 and isinstance(value, six.text_type): # Python 2:

value = value.encode('utf-8')

#value = email.utils.encode_rfc2231(value, 'utf-8')

#value = '%s*=%s' % (name, value)

value = '%s="%s"' % (name, value)

return value

两行注释是原来的书写方式

### 回答1: 为了在 Python 3.6 中发送 HTTP/2 POST 请求,你需要安装并使用一个第三方,比如 httplib2 或者 hyper。 下面是使用 httplib2 发送 HTTP/2 POST 请求的示例代码: ``` import httplib2 h = httplib2.Http('.cache', disable_ssl_certificate_validation=True) h.add_credentials('username', 'password') response, content = h.request( 'https://http2.example.com/post', 'POST', headers={'Content-Type': 'application/x-www-form-urlencoded'}, body='key1=value1&key2=value2' ) print(response.status) print(content) ``` 如果你想使用 hyper 发送 HTTP/2 POST 请求,可以使用下面的代码: ``` import hyper s = hyper.Session() r = s.post( 'https://http2.example.com/post', headers={'Content-Type': 'application/x-www-form-urlencoded'}, data='key1=value1&key2=value2' ) print(r.status_code) print(r.text) ``` 希望这些信息对你有帮助! ### 回答2: 要在Python 3.6中发送HTTP/2的POST请求,首先需要确保安装了相应的模块,比如`hyper`或`httpx`。以下是使用`httpx`模块发送HTTP/2的POST请求的示例代码: ```python import httpx async def post_request(): url = 'https://example.com' # 替换为目标网址 async with httpx.AsyncClient(http2=True) as client: payload = {'key1': 'value1', 'key2': 'value2'} # 替换为实际的请求参数 headers = {'Content-Type': 'application/x-www-form-urlencoded'} # 替换为实际的请求头部 response = await client.post(url, data=payload, headers=headers) print(response.text) # 需要使用asyncio运行异步函数 import asyncio asyncio.run(post_request()) ``` 在上述代码中,`httpx`模块提供了HTTP/2的支持,通过设置`http2=True`参数来启用HTTP/2协议。`post_request()`函数是异步函数,使用`asyncio.run()`函数来运行。 请注意,这仅是一个简单的示例,实际情况下,您可能需要根据目标网址的要求和其他需要的参数进行相应的修改。 ### 回答3: 在Python 3.6中,要发送HTTP/2的POST请求,可以使用`http.client`模块的`HTTPConnection`类。这个类提供了一种连接到HTTP服务器并发送请求的方法。 首先,我们需要创建一个HTTPConnection对象,将目标主机的服务器地址和端口作为参数传递给它。然后,我们可以使用request()方法来发送POST请求。我们还需要设置一些请求标头,例如Content-Type和Content-Length。 以下是一个示例代码的基本结构,展示了如何发送HTTP/2的POST请求: ```python import http.client # 创建HTTPConnection对象 conn = http.client.HTTPConnection("目标服务器地址", port=端口号) # 设置请求标头 headers = { 'content-type': 'application/json', 'content-length': '长度' } # 设置请求体 body = { 'key1': 'value1', 'key2': 'value2' } # 发送POST请求 conn.request("POST", "/目标路径", body, headers) # 获取响应 res = conn.getresponse() # 打印响应状态码和内容 print(res.status, res.read().decode()) ``` 请注意,以上示例代码只是一个基本结构,需要根据你的具体需求进行进一步修改。例如,将目标服务器地址、端口号和其它请求参数替换为正确的值。 此外,在使用以上代码之前,你需要在Python环境中安装相应的依赖包,例如`hyper`、`h2`等,以确保支持HTTP/2协议。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值