简体   繁体   English

Python请求发送带有二进制数据的文本

[英]Python requests send text with binary data

I'm trying to send both binary data and a text string (or any other normal variable) between servers using python's requests library. 我正在尝试使用python的请求库在服务器之间发送二进制数据和文本字符串(或任何其他常规变量)。

I have two servers talking to each other. 我有两台服务器互相通信。 Server A sends a GET request via python's requests library. 服务器A通过python的请求库发送GET请求。 It looks something like this: 看起来像这样:

color, file_data = requests.get('https://www.serverb.com/testing', params = {...})

Server B then does something like: 服务器B然后执行以下操作:

var1 = request.args.get('var1')
var2 = request.args.get('var2')

# This function creates a binary file, saves it to disk, and returns the file name.
res = some_function(var1, var2)

file_data = codecs.open(res, 'rb').read()

return 'Success!', file_data

However, when I attempt this, Server B throws a 500 error. 但是,当我尝试这样做时,服务器B抛出500错误。

Is what I'm attempting to do impossible? 我试图做的事是不可能的吗? If I can only send back the binary data, but with custom headers, that would work as well, and help on how to set those up would be appreciated. 如果我只能使用自定义标头发送回二进制数据,那也可以,并且对如何设置这些标定会有所帮助。

Thanks in advance. 提前致谢。

An HTTP response looks like this: HTTP响应如下所示:

HTTP/1.1 200 OK
<headers>
<blank line>
<response body>

The <blank line> generally contains these characters - \\r\\n , also known as CRLF . <blank line>通常包含以下字符- \\r\\n ,也称为CRLF

So, when a server sends a response, the client (or browser) can differentiate from the headers and the response body by looking at the <blank line> . 因此,当服务器发送响应时,客户端(或浏览器)可以通过查看<blank line>与报头和响应正文区分开。 So, anything after <blank line> is treated as the response body. 因此, <blank line>之后的任何内容都将被视为响应主体。

What that means is, there can be at most 1 response body only . 这意味着,最多只能1个响应主体 So, even if you think you're sending color and file_data separately like you're doing, they're still going in one response and the client will treat them as a single body. 因此,即使您认为要像发送操作一样分别发送colorfile_data ,它们仍会在一个响应中进行,客户端会将它们视为一个整体。


What you can do to get around this? 您可以做什么来解决这个问题?

You'll need a way to know which part of the response body is the color and which is file_data . 您需要一种方法来知道响应主体的哪一部分是color ,哪一部分是file_data You can do that by sending a custom header along with the response. 您可以通过发送自定义标头以及响应来实现。

Let's say, your response looks like this: 假设您的回复如下所示:

HTTP/1.1 200 OK
<headers>
<blank line>
<color><file_data>

Now, you can set a custom header on your response which will tell you how many bytes are there of the color variable. 现在,您可以在响应中设置自定义标头,该标头将告诉您color变量有多少字节。

For example, if the value of color is yellow and it's 6 characters, that means it has 6 bytes. 例如,如果color值为yellow并且是6个字符,则表示它具有6个字节。 So, you can set a custom header that would tell you the total length of color : 因此,您可以设置一个自定义标题,该标题将告诉您color的总长度:

HTTP/1.1 200 OK
<headers>
X-Color-Length: 6
<blank line>
<color><file_data>

Then, in your client side, you can get the total bytes from the X-Color-Length header and read that number of bytes from the body to get the value of the color . 然后,在客户端,您可以从X-Color-Length标头中获取总字节,并从正文中读取该字节数以获取color的值。 Example: 例:

response = requests.get(...)

color_len = response.headers['X-Color-Length']

color = response.content[:color_len]
file_data = response.content[color_len:]

UPDATE: Now that I think about it, I've made this a little more complex than it should be. 更新:现在我考虑了一下,我已经使它比应该的要复杂一些。 You can just send the color's value directly in a header like X-Color: <color> , instead of the number of bytes it has. 您可以直接在X-Color: <color>类的标头中直接发送颜色的值,而不是发送它具有的字节数。

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

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