473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Developing a network protocol with Python


Hello,

I would like to develop a new network protocol, where the server and the
clients are Python programs.
I think to be effective, I need to use TCP_NODELAY, and manually
buffered transfers.
I would like to create a general messaging object that has methods like

sendinteger
recvinteger
sendstring
recvstring

To be more secure, I think I can use this loads function to transfer
more elaborate python stuctures:

def loads(s):
"""Loads an object from a string.

@param s: The string to load the object from.
@return: The object loaded from the string. This function will not
unpickle globals and instances.
"""
f = cStringIO.StringIO(s)
p = cPickle.Unpickler(f)
p.find_global = None
return p.load()

Am I on the right way to develop a new protocol?
Are there any common mistakes that programmers do?
Is there a howto where I can read more about this?

Thanks

Les
Dec 12 '05 #1
12 5492
> Am I on the right way to develop a new protocol?
Are there any common mistakes that programmers do?
Is there a howto where I can read more about this?


If you _must_ develop your own protocol, use at least twisted. But I'd
go for an existing solutions out there - namely pyro. No need to invent
wheels again :)

Regards,

Diez
Dec 12 '05 #2
On Mon, 12 Dec 2005, Laszlo Zsolt Nagy wrote:
I think to be effective, I need to use TCP_NODELAY, and manually
buffered transfers.
Why?
I would like to create a general messaging object that has methods like

sendinteger
recvinteger
sendstring
recvstring
Okay. So you're really developing a marshalling layer, somewhere between
the transport and application layers - fair enough, there are a lot of
protocols that do that.
To be more secure,
Do you really mean secure? I don't think using pickle will give you
security. If you want security, run your protocol over an TLS/SSL
connection.

If, however, you mean robustness, then this is a reasonable thing to do -
it reduces the amount of code you have to write, and so reduces the number
of bugs you'll write! One thing to watch out for, though, is the
compatibility of the pickling at each end - i have no idea what the
backwards- and forwards-compatibility of the pickle protocols is like, but
you might find that if they're on different python versions, the ends
won't understand each other. Defining your own protocol down to the
bits-on-the-socket level would preclude that possibility.
I think I can use this loads function to transfer more elaborate python
stuctures:

def loads(s):
"""Loads an object from a string.
@param s: The string to load the object from.
@return: The object loaded from the string. This function will not
unpickle globals and instances.
"""
f = cStringIO.StringIO(s)
p = cPickle.Unpickler(f)
p.find_global = None
return p.load()
I don't know the pickle module, so i can't comment on the code.
Am I on the right way to develop a new protocol?
Aside from the versioning issue i mention above, you should bear in mind
that using pickle will make it insanely hard to implement this protocol in
any language other than python (unless someone's implemented a python
pickle library in it - is there such a beast for any other language?).
Personally, i'd steer clear of doing it like this, and try to use an
existing, language-neutral generic marshalling layer. XML and ASN.1 would
be the obvious ones, but i wouldn't advise using either of them, as
they're abominations. JSON would be a good choice:

http://www.json.org/

If it's expressive enough for your objects. This is a stunningly simple
format, and there are libraries for working with it for a wide range of
languages.
Are there any common mistakes that programmers do?
The key one, i'd say, is not thinking about the future. Make sure your
protocol is able to grow - use a version number, so peers can figure out
what language they're talking, and perhaps an option negotiation
mechanism, if you're doing anything complex enough to warrant it (hey, you
could always start without it and add it in a later version!). Try to
allow for addition of new commands, message types or whatever, and for
extension of existing ones (within reason).
Is there a howto where I can read more about this?


Not really - protocol design is a bit of a black art. Someone asked about
this on comp.protocols.tcp-ip a while ago:

http://groups.google.co.uk/group/com...ca111d67768b83

And didn't get much in the way of answers. Someone did point to this,
though:

http://www.internet2.edu/~shalunov/w...ol-design.html

Although i don't agree with much of what that says.

tom

--
limited to concepts that are meta, generic, abstract and philosophical --
IEEE SUO WG
Dec 13 '05 #3
Tom Anderson wrote:
I think to be effective, I need to use TCP_NODELAY, and manually
buffered transfers.

Why?

Because of the big delays when sending small messages (size < 1500 bytes).

Personally, i'd steer clear of doing it like this, and try to use an
existing, language-neutral generic marshalling layer. XML and ASN.1 would
be the obvious ones, but i wouldn't advise using either of them, as
they're abominations. JSON would be a good choice:

http://www.json.org/

I need to send Python objects too. They are too elaborate to convert
them to XML. (They are using cyclic weak references and other Python
specific stuff.) I can be sure that on both sides, there are Python
programs. Is there any advantage in using XML if I already need to send
Python objects? Those objects cannot be represented in XML, unless
pickled into a CDATA string.
And didn't get much in the way of answers. Someone did point to this,
though:

http://www.internet2.edu/~shalunov/w...ol-design.html

Hmm, this was very helpful. Thank you!

Les

Dec 13 '05 #4
Il 2005-12-12, Laszlo Zsolt Nagy <ga*****@designaproduct.biz> ha scritto:

Hello,

I would like to develop a new network protocol, where the server and the
clients are Python programs.
You should use Twisted for this:

Writing clients
http://twistedmatrix.com/projects/co...o/clients.html

Writing servers
http://twistedmatrix.com/projects/co...o/servers.html

I think to be effective, I need to use TCP_NODELAY, and manually
buffered transfers.
I would like to create a general messaging object that has methods like

sendinteger
recvinteger
sendstring
recvstring
You can inherit from twisted.internet.protocol.Protocol or one of its
subclasses, they handle buffering and all sort of these things for
you. Cannot have to reinvent the wheel.
To be more secure, I think I can use this loads function to transfer
more elaborate python stuctures:

def loads(s):
"""Loads an object from a string.

@param s: The string to load the object from.
@return: The object loaded from the string. This function will not
unpickle globals and instances.
"""
f = cStringIO.StringIO(s)
p = cPickle.Unpickler(f)
p.find_global = None
return p.load()


Using untrusted pickle loading is *NOT* more secure:
http://www.python.org/doc/2.2.3/lib/pickle-sec.html
and
http://www.livejournal.com/users/jcalderone/15864.html
--
Lawrence - http://www.oluyede.org/blog
"Anyone can freely use whatever he wants but the light at the end
of the tunnel for most of his problems is Python"
Dec 13 '05 #5
Il 2005-12-13, Laszlo Zsolt Nagy <ga*****@designaproduct.biz> ha scritto:
I need to send Python objects too. They are too elaborate to convert
them to XML. (They are using cyclic weak references and other Python
specific stuff.) I can be sure that on both sides, there are Python
programs. Is there any advantage in using XML if I already need to send
Python objects? Those objects cannot be represented in XML, unless
pickled into a CDATA string.


If you have to send Python objects over the wire the best way is to have
twisted on both ends and use twisted's PerspectiveBroker, see:
http://twistedmatrix.com/projects/co.../pb-intro.html

--
Lawrence - http://www.oluyede.org/blog
"Anyone can freely use whatever he wants but the light at the end
of the tunnel for most of his problems is Python"
Dec 13 '05 #6
Laszlo Zsolt Nagy wrote:

I need to send Python objects too. They are too elaborate to convert
them to XML. (They are using cyclic weak references and other Python
specific stuff.) I can be sure that on both sides, there are Python
programs. Is there any advantage in using XML if I already need to send
Python objects? Those objects cannot be represented in XML, unless
pickled into a CDATA string.

Try Pyro http://pyro.sourceforge.net
before rolling your own Python-specific protocol.

--Irmen
Dec 14 '05 #7
Try Pyro http://pyro.sourceforge.net
before rolling your own Python-specific protocol.

You are right. I wanted to use pyro before, because it is well tested
and it has nice features.
Unfortunately, it is not good for me. :-(

I already have my own classes. My objects are in object ownership trees,
and they are referencing to each other (weakly and strongly). These
classes have their own streaming methods, and they can be pickled
safely. This is something that pyro cannot handle. It cannot handle
these ownership object trees with weak references. Pyro can distribute
distinct objects only, and it uses an 'object naming scheme'. This is
not what I want. My objects do not have a name, and I do not want to
create 'technical names' just to force the Pyro style access. (Another
problem is that I do not want to rewrite my classes and inherit them
from the Pyro base object class.)

Thanks for the comment. I'm going to check the Pyro documentation again.
I might find something useful.

Les

Dec 14 '05 #8
Laszlo Zsolt Nagy <ga*****@designaproduct.biz> writes:
I already have my own classes. My objects are in object ownership
trees, and they are referencing to each other (weakly and
strongly). These classes have their own streaming methods, and they
can be pickled safely.


Standard warning: if you're accepting requests from potentially
hostile sources, don't use pickle.
Dec 14 '05 #9
Paul Rubin wrote:
Laszlo Zsolt Nagy <ga*****@designaproduct.biz> writes:

I already have my own classes. My objects are in object ownership
trees, and they are referencing to each other (weakly and
strongly). These classes have their own streaming methods, and they
can be pickled safely.


Standard warning: if you're accepting requests from potentially
hostile sources, don't use pickle.

Yes, I know. Not talking about TLS/SSL - there can be hostile persons,
knowing a valid password and using a modified client program.

But how can I transfer pure python objects otherwise? Pyro also uses
Pickle and it also transfers bytecode.
Well, Pyro has an option to use XML messaging, but that is very
restricted, you cannot publish arbitrary python objects with XML. :-(

I read somewhere that Pickle had a security problem before Python 2.2,
but after 2.2 it has been solved.
BTW how CORBA or COM does this? They can do object marshaling safely.
Can we do the same with Python?
Isn't it enough to implement find_global of a cPickler ?

Les
Dec 14 '05 #10
Laszlo Zsolt Nagy <ga*****@designaproduct.biz> writes:
But how can I transfer pure python objects otherwise? Pyro also uses
Pickle and it also transfers bytecode.
Pyro in the past used pickle in an insecure way. I'd heard it had
been fixed and I didn't realize it still uses pickle.
I read somewhere that Pickle had a security problem before Python 2.2,
but after 2.2 it has been solved.
If you use pickle in the obvious way, it's definitely still insecure.
There is some info in the docs about how to use some special pickle
features to protect yourself from the insecurity, but you have to go
out of your way for that. I'm skeptical that they really protect you
in all cases, so I'd avoid unpickling any untrusted data. But I don't
know a specific exploit
BTW how CORBA or COM does this? They can do object marshaling safely.
I think they don't let you marshal arbitrary class instances and have
the class constructors called as part of demarshalling (COM anyway, I
don't know about CORBA).
Can we do the same with Python?
Yes, of course, it's possible in principle, but pickle doesn't do it
that way.

See SF RFE #467384 and bug #471893 for some more discussion of this.
Basically I think these issues are better understood now than they
were a few years ago.
Isn't it enough to implement find_global of a cPickler ?


I can't definitely say the answer is no but I feel quite paranoid
about it. The cPickle code needs careful review which I don't think
it's gotten. It was not written with security in mind, though some
security hacks were added as afterthoughts. I continue to believe
that Python should have a deserializer designed to be absolutely
bulletproof no matter what anyone throws at it, and it doesn't
currently have one. I've gotten by with limited, ad hoc wire formats
for the applications where I've needed them.
Dec 14 '05 #11
Paul Rubin wrote:
Laszlo Zsolt Nagy <ga*****@designaproduct.biz> writes:

But how can I transfer pure python objects otherwise? Pyro also uses
Pickle and it also transfers bytecode.

Pyro in the past used pickle in an insecure way. I'd heard it had
been fixed and I didn't realize it still uses pickle.

On the features page, you can read this:

"Mobile objects. Clients and servers can pass objects around - even when
the server has never known them before. Pyro will then automatically
transfer the needed Python bytecode."

I believe that using cPickle and transferring data (but not the code) is
still more secure than transferring bytecode. :-)

Les

Dec 15 '05 #12
Laszlo Zsolt Nagy wrote:
"Mobile objects. Clients and servers can pass objects around - even when
the server has never known them before. Pyro will then automatically
transfer the needed Python bytecode."

I believe that using cPickle and transferring data (but not the code) is
still more secure than transferring bytecode. :-)


Note that the mobile *code* feature of Pyro is off by default.
And that the transfer of bytecodes is only part of the "problem",
because it is possible to craft special constructed pickle streams
that will do nasty things on the receiving side....

--Irmen
Dec 15 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tom Plunket | last post by:
Hey all- A friend of mine has built this interesting (to me) bit of hardware that's intended to communicate with a PC over ethernet. The hardware has an OS written in C, and has some PC-side...
14
by: DaveC | last post by:
I know this is the wrong group but I need a little pointing in a direction please. I need to write an implementation of a yet to be published protocol. It is transported over the internet via...
6
by: John Walton | last post by:
Hello, everyone. I just began school, and they already assigned us science fair. Since I'm in 8th grade, I get to do demonstrations for our projects. I'm probably going to demonstrate Python's...
4
by: Roland Hedberg | last post by:
Hi! I need a fast protocol to use between a client and a server, both sides written i Python. What the protocol has to accomplish is extremely simple; the client sends a number of lines...
14
by: David W. Fenton | last post by:
I'm no stranger to this error message, but I have a client who is experiencing it, but, fortunately, without any actual data corruption, and it's driving them made. Their inability to grasp that...
4
by: Ivan Voras | last post by:
I have a simple network protocol client (it's a part of this: http://sqlcached.sourceforge.net) implemented in Python, PHP and C. Everything's fine, except that the Python implementation is the...
15
by: diffuser78 | last post by:
I am a newbie in python. I want to learn and implement a small networking concept. Please help me. Every help is appreciated. I have one Linux Box and one Windows PC. I want to have a daemon...
3
by: DanielJohnson | last post by:
I was wondering if anyblody can suggest me a network simulator written in python in which I can add on my own code and extend its functionality. I am looking for a simulator which will simualte...
4
by: aki | last post by:
Hi all, i am writing codes for implementing network protocols. i have written a method which is receiving a packet from network. i have assumed that the packet i will receive will be of type...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.