简体   繁体   English

(Python)通过网络发送结构化数据包

[英](Python) Send structured packet over network

i want to build a chat application which supports text messaging, group messaging, file transfer(like netmeeting). 我想构建一个聊天应用程序,该应用程序支持文本消息传递,组消息传递,文件传输(例如netmeeting)。 when i send it over TCP socket i saw that data is not structured all the data send as string over TCP. 当我通过TCP套接字发送它时,我看到数据不是结构化的,所有数据都通过TCP以字符串形式发送。 I want to send it in a structured way with few headers like name:,ip:,data:,data_type_flag:(file or text message) etc... one stackoverflow member told me to use TELEPATHY but i can't get a simple tutorial to understand. 我想以结构化的方式发送它,并使用一些标头,例如name:,ip:,data:,data_type_flag :(文件或文本消息)等...一个stackoverflow成员告诉我使用TELEPATHY,但我无法简单教程了解。 how can i send structured data over socket? 如何通过套接字发送结构化数据? or can any one suggest me a good tutorial to implement telepathy properly. 或可以给我推荐一个很好的教程来正确实现心灵感应。 i want to communicate over network as peer-to-peer rather than dedicated server.. Thanks 我想通过网络作为对等而不是专用服务器进行通信。

Try google protcol buffers or apache thrift . 尝试使用Google Protcol缓冲区Apache Thrift There are many examples for how to use them. 有许多使用它们的示例。

As for your comment about "peer to peer", please realize that even in peer-to-peer one of the peers is always acting as a server (sometimes both are). 至于您对“点对点”的评论,请意识到即使在点对点中,也有一个对等点始终充当服务器(有时两者都是)。

TCP is a transport layer protocol, as opposed to application layer. TCP是一个传送层协议,而不是应用层。 This means that TCP is not responsible for the types of data you send, only the raw bits. 这意味着TCP不对您发送的数据类型负责,仅对原始位负责。 HTTP has headers and other metadata because it is application level. HTTP具有标头和其他元数据,因为它是应用程序级别的。

For a project like the one you're talking about, you will want to implement your own application layer protocol, but this is not exactly a trivial task. 对于一个您正在谈论的项目,您将需要实现自己的应用程序层协议,但这并不是一项微不足道的任务。 I would look at the python source code in the httplib module for an example of how to implement such a protocol, but note that this is likely fairly different still from what you want, as you will want persistent socket connections to be a first-class citizen in a peer-to-peer chat protocol like the one you're describing. 我将在httplib模块中查看python源代码,以获取有关如何实现此协议的示例,但请注意,这可能与您想要的仍然完全不同,因为您希望持久性套接字连接是一流的点对点聊天协议(例如您所描述的协议)中的公民。

Another option is to use one of the various RPC libraries, eg xmlrpclib , which will handle a decent amount of the required low-level network things for you (although not file transfer; there are other libraries like the ftplib that can do this). 另一个选择是使用各种RPC库之一,例如xmlrpclib ,它将为您处理相当数量的所需低级网络事物(尽管不是文件传输;还有其他库,例如ftplib可以做到这一点)。

Pickle your data before you send it, and unpickle it on the other end? 在发送数据之前先对数据进行腌制,然后在另一端进行腌制? http://docs.python.org/library/pickle.html http://docs.python.org/library/pickle.html

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

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