简体   繁体   English

在python请求数据中发送“\\r\\n”符号

[英]Send “\r\n” symbols in python requests data

Everything ok with curl: curl 一切正常:

curl -v "http://user:password@localhost/_control.html" -d $'data1=1\r\n'

I tried this way in python:我在python中尝试过这种方式:

url = "http://localhost/_control.html"

payload = {'data1': '1\r\n'}

headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}

r = requests.post(url, data=payload, headers=headers, auth=('user', 'password'))

But it doesn't work.但它不起作用。 Content-length in this case is 13 instead of 9 (with curl request)在这种情况下,内容长度是 13 而不是 9(带有 curl 请求)

Is it possible to send same data (with \\r\\n at the end) using python requests?是否可以使用 python 请求发送相同的数据(以 \\r\\n 结尾)?

The \\r and \\n characters are being URL-encoded, as they should be, because application/x-www-form-urlencoded data cannot contain those characters directly : \\r\\n字符是 URL 编码的,因为它们应该是,因为application/x-www-form-urlencoded数据 不能直接包含这些字符

Non-alphanumeric characters are replaced by %HH , a percent sign and two hexadecimal digits representing the ASCII code of the character.非字母数字字符替换为%HH 、一个百分号和两个表示字符 ASCII 代码的十六进制数字。 Line breaks are represented as "CR LF" pairs (ie, %0D%0A ).换行符表示为“CR LF”对(即%0D%0A )。

Logging the request sent from Python using this technique , we can see that it correctly sends these 13 bytes:使用这种技术记录从 Python 发送的请求,我们可以看到它正确发送了这 13 个字节:

data1=1%0D%0A

In the case of curl , its manual page makes no mention of encoding of whatever you pass to -d / --data , so presumably you're expected to encode the string yourself before passing it to curl .curl的情况下,它的手册页没有提到任何你传递给-d / --data的编码,所以大概你应该在将它传递给curl之前自己编码字符串。 We can confirm this with --trace-ascii - :我们可以通过--trace-ascii -来确认这一点:

=> Send data, 9 bytes (0x9)
0000: data1=1

The \\r\\n pair doesn't show up clearly here, but we can infer that it's not encoded because of the byte count. \\r\\n对在这里没有清楚地显示出来,但我们可以推断它没有因为字节数而被编码。

In short, the request you are sending with that curl command is not valid to begin with.简而言之,您使用curl命令发送的请求一开始就无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM