Reputation: 4959
So I have to create a message protocol that works like this:
codFunc arg1 arg2...
ex:
0 'hello world'
10 'user' 'password'
Right now I concatenate to send, and use string.split
to read, but for several reasons, that is not the best way.
So my question is, what's the best way to create the protocol? What existing protocols should I use?
Thanks.
Upvotes: 0
Views: 1481
Reputation: 14497
What is wrong with split is that if your username contains white-space, it will be splited into separate arguments.
Split it one time to get responsible function number:
num, args = s.split(None, 1)
Parse string into arguments, maybe with shlex
:
import shlex
argv = shlex.split(args)
Remove surrounding quotes:
argv = [s.strip(shlex.quotes) for s in argv]
Call your function:
myfunc(*argv)
Upvotes: 2
Reputation: 7187
shlex is probably good, split has problems with quoted whitespace, pickle is insecure. JSON is good.
I like to use: https://www.google.com/search?gcx=c&ix=c1&sourceid=chrome&ie=UTF-8&q=bufsock ...with ASCII data that's null terminated or something, to anchor parts of the protocol.
Bear in mind that there's not always a one to one relationship between send()'s and recv()'s. It's easy to get complacent about this, but it can cause reliability problems under network load.
Upvotes: 2