如何使用 Python 传输二进制数据?

发布于 2024-07-18 07:31:26 字数 609 浏览 3 评论 0原文

我是第一次开发客户端-服务器程序,但遗憾的是我不知道从哪里开始我正在做的事情。

我将使用 Google Protocol Buffers 在客户端和服务器之间传输二进制数据。 我将使用 Python 变体。 据我了解,基本思想是客户端将数据序列化,将其发送到服务器,然后服务器将数据反序列化。

问题是,我真的不知道从哪里开始向服务器发送二进制数据。 我希望它是像 HTTP 请求这样“简单”的东西,但我一直在 Google 上搜索传输二进制数据的方法,并迷失在大量的教程、指南和文档中。 我什至无法通过调查 HTTP 传输来判断我是否找错了树(我希望使用它,因此如果需要安全性,我可以将其提升到 HTTPS)。 不过,我真的不想进入套接字编程的水平 - 我想在转向之前使用可用的库。 (我也更喜欢标准的 Python 库,不过如果有完美的第 3 方库,我就会活下去。)

所以,如果有人对如何传输二进制文件有一个好的起点(或者想自己解释一下)通过Python获取数据,我将不胜感激。 顺便说一句,我正在运行的服务器当前正在运行带有 mod_python 的 Apache。

I'm working on a client-server program for the first time, and I'm feeling woefully inadequate on where to begin for what I'm doing.

I'm going to use Google Protocol Buffers to transfer binary data between my client and my server. I'm going to be using the Python variant. The basic idea, as I understand, is that the client will serialize the data, send it to the server, which will then deserialize the data.

The problem is, I'm really not sure where to begin for sending binary data to the server. I was hoping it'd be something "simple" like an HTTP request, but I've been searching around Google for ways to transfer binary data and getting lost in the vast multitude of tutorials, guides and documentation. I can't even tell if I'm barking up the wrong tree by investigating HTTP transfers (I was hoping to use it, so I could knock it up a notch to HTTPS if security is necessary). I really don't want to have to go to the level of socket programming, though - I'd like to use the libraries available before turning to that. (I'd also prefer standard Python libraries, though if there's the perfect 3rd party library I'll live.)

So, if anyone has a good starting point (or wants to explain a bit themselves) on how a good way to transfer binary data via Python, I'd be grateful. The server I'm running is currently running Apache with mod_python, by the way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天生の放荡 2024-07-25 07:31:26

任何时候您要将二进制数据从一个系统移动到另一个系统时,都需要记住以下几点。

不同的机器以不同的方式存储相同的信息。 这对内存和网络都有影响。 更多信息请点击这里 (http://en.wikipedia.org/wiki/Endianness)

因为你如果你正在使用 python,你可以在这里放松一下(假设客户端和服务器都使用 python),只需使用 cPickle 来序列化你的数据。 如果你真的想要二进制,你必须熟悉 python 的 struct 模块(http: //docs.python.org/library/struct.html)。 并了解如何打包/解包数据。

我会首先从简单的线路协议服务器开始,直到您克服网络通信的困难。 如果您以前从未这样做过,那么很快就会感到困惑。 如何发出命令,如何传递数据,如何在错误时重新同步等等...

如果您已经了解客户端/服务器协议设计的基础知识,那么首先练习在磁盘上打包和解包二进制结构。 对于此类情况,我还参考了 HTTP 和 FTP 的 RFC。

--------根据评论进行编辑--------
通常,这种事情是通过向服务器发送一个“标头”来完成的,其中包含文件的校验和以及文件的大小(以字节为单位)。 请注意,我指的不是 HTTP 标头,您可以根据需要自定义它。 事件链需要像这样......

CLIENT: "UPLOAD acbd18db4cc2f85cedef654fccc4a4d8 253521"
SERVER: "OK"
(server splits the text line to get the command, checksum, and size)
CLIENT: "010101101010101100010101010etc..." (up to 253521 bytes)
(server reasembles all received data into a file, then checksums it to make sure it matches the original)
SERVER: "YEP GOT IT"
CLIENT: "COOL CYA"

这过于简化,但我希望你能明白我在这里所说的内容。

Any time you're going to move binary data from one system to another there a couple of things to keep in mind.

Different machines store the same information differently. This has implication both in memory and on the network. More info here (http://en.wikipedia.org/wiki/Endianness)

Because you're using python you can cut yourself some slack here (assuming the client and server will both by in python) and just use cPickle to serialize your data. If you really want binary, you're going to have to get comfortable with python's struct module (http://docs.python.org/library/struct.html). And learn how to pack/unpack your data.

I would first start out with simple line-protocol servers until you get past the difficulty of network communication. If you've never done it before it can get confusing very fast. How to issue commands, how to pass data, how to re-sync on errors etc...

If you already know the basics of client/server protocol design, then practice packing and unpacking binary structures on your disk first. I also refer to the RFCs of HTTP and FTP for cases like this.

-------EDIT BASED ON COMMENT--------
Normally this sort of thing is done by sending the server a "header" that contains a checksum for the file as well as the size of the file in bytes. Note that I don't mean an HTTP header, you can customize it however you want. The chain of events needs to go something like this...

CLIENT: "UPLOAD acbd18db4cc2f85cedef654fccc4a4d8 253521"
SERVER: "OK"
(server splits the text line to get the command, checksum, and size)
CLIENT: "010101101010101100010101010etc..." (up to 253521 bytes)
(server reasembles all received data into a file, then checksums it to make sure it matches the original)
SERVER: "YEP GOT IT"
CLIENT: "COOL CYA"

This is overly simplified, but I hope you can see what I'm talking about here.

高跟鞋的旋律 2024-07-25 07:31:26

我不确定你的问题是否正确,但也许你可以看看扭曲项目

正如您在常见问题解答中所看到的,“Twisted 是一个用 Python 编写的网络引擎,支持多种协议。它包含一个 Web 服务器、众多聊天客户端、聊天服务器、邮件服务器等等。Twisted 由许多子组件组成-可以单独访问的项目[...]”。

文档非常好,并且互联网上有很多示例。 希望能帮助到你。

I'm not sure I got your question right, but maybe you can take a look at the twisted project.

As you can see in the FAQ, "Twisted is a networking engine written in Python, supporting numerous protocols. It contains a web server, numerous chat clients, chat servers, mail servers, and more. Twisted is made up of a number of sub-projects which can be accessed individually[...]".

The documentation is pretty good, and there are lots of examples on the internet. Hope it helps.

微凉徒眸意 2024-07-25 07:31:26

我想这取决于您与 Google Protocol Buffers 的联系程度,但您可能想查看 Thrift

Thrift 是一个软件框架
可扩展的跨语言服务
发展。 它结合了一个软件
与代码生成引擎堆栈
构建高效运行的服务
C++、Java 之间无缝连接,
Python、PHP、Ruby、Erlang、Perl、
Haskell、C#、Cocoa、Smalltalk 和
OCaml。

他们的主页上有一个很好的入门示例。

I guess it depends on how tied you are to Google Protocol Buffers, but you might like to check out Thrift.

Thrift is a software framework for
scalable cross-language services
development. It combines a software
stack with a code generation engine to
build services that work efficiently
and seamlessly between C++, Java,
Python, PHP, Ruby, Erlang, Perl,
Haskell, C#, Cocoa, Smalltalk, and
OCaml.

There's a great example for getting started on their home page.

日久见人心 2024-07-25 07:31:26

一个简单的问题:为什么是二进制? 有效负载本身是二进制的,还是您只是更喜欢二进制格式?
如果是前者,也可以将 base64 编码与 JSON 或 XML 结合使用; 它确实使用了更多的空间(~34%),并且处理开销更多,但对于许多用例来说不一定足够重要。

One quick question: why binary? Is the payload itself binary, or do you just prefer a binary format?
If former, it's possible to use base64 encoding with JSON or XML too; it does use more space (~34%), and bit more processing overhead, but not necessarily enough to matter for many use cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文