English 中文(简体)
使用python UDP数据包发送字符串数据的最佳方式?
原标题:Best way to send string data using python UDP packets?

前言:我对python很陌生(大约7天),但我是一名经验丰富的软件工程师。

我想在运行python脚本的机器之间发送数据。我的想法(为了简化事情)是将数据(字符串和int)连接到一个字符串中,并在客户端进行解析。

UDP数据包用简单的字符串发送得很漂亮,但当我试图发送有用的数据时,python总是抱怨我发送的数据;特别是python不允许我连接元组。

  • In order to parse the data on the client I need to seperate the data with a dash character: - .
  • nodeList属于dictionary类型,其中键是字符串,值是双精度。

    randKey = random.choice( nodeList.keys() )
    data = str(randKey) + - + str(nodeList[randKey])
    mySocket.sendto ( data , address ) 
    

上面的代码会产生以下错误:

TypeError:强制为Unicode:需要字符串或缓冲区,找到元组

我不明白为什么它认为它是我试图连接的元组。。。

所以我的问题是,我如何纠正这一点,让Python满意,或者有人能建议我用更好的方式发送数据?

提前谢谢。

最佳回答

我强烈建议使用Google协议缓冲区在Python中实现为protobuf,因为它将处理行两端的序列化。它有Python绑定,使您能够轻松地将其与现有Python程序一起使用。

使用您的示例代码,您将创建一个.proto文件,如下所示:

message SomeCoolMessage {
    required string key = 1;
    required double value = 2;
}

生成后,您可以这样使用它:

randKey = random.choice( nodeList.keys() )
data = SomeCoolMessage()
data.key = randKey
data.value = nodeList[randKey]
mySocket.sendto ( data.SerializeToString() , address ) 
问题回答

我可能会使用json模块序列化数据。

您需要序列化数据。Pickle为您内置了这一功能,您可以要求Pickle提供数据与二进制数据的ascii表示(请参阅文档),也可以使用json(它还为您序列化数据),两者都在标准库中。但实际上,有十万个不同的库为您处理从一台机器到另一台机器的所有工作。我建议使用图书馆。

Depending on speed, etc. there are different trade offs for the various libraries. In the standard library you get HTTP, that s about it (well and raw sockets). But there are others. If super fast speed is more important than other things..., zeroMQ, or google s protocol buffers might be valid options.

对我来说,我使用rpyc通常,它会让我变得完全懒惰,只需通过网络呼叫另一个进程。通常情况下,它足够快。

您知道UDP不能保证数据会显示在另一侧,也不能保证它会按顺序显示。对于你的申请,你可能不在乎,我不知道,但我只是想我会把它提出来。





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签