views:

470

answers:

4

Hi stackers,

I'm programming a simple network chat with a Python server and a Java client. But one question came into my mind:

Which "network protocol" should I use for communication? There are some possibilities for me:

  • YAML: Nice to parse, problem: parsed objects contain language specific parts
  • XML: Easy to parse, big overhead for simple tasks
  • create an own "language": Problems with escaping, but most flexible

So what is the best practice for this? Are there other alternatives?

A: 

You should have a look at this thread

soulmerge
+6  A: 

Check JSON. It is compatible accross many languages (Python and Java included), and it is human readable. http://www.json.org/

If you plan to do Web development, and plan to use Javascript, then JSON might be a good choice as it was originally designed for Javascript. Moreover compared to YAML, using JSON in Python is as easy as writing: import json (it is part of the standard library).

You may have a look at the following page, comparing XML, JSON, and YAML. It seems they are differences in terms of encoding delay and memory used, that might guide your choice.

Mapad
Also, XML is most definitely not easy to parse. Not as easy to parse as JSON or YAML.
S.Lott
Just thought I'd add that I wrote the above linked page comparing XML, JSON and YAML. Well, it doesn't compare them but merely introduces the need for a comparison.I researched the subject for some months, carried out way too many tests and have lots of lovely data to share.I'll be publishing a series of articles on the subject from mid-May 2009 onwards.
Jon Cram
+4  A: 

It may be a little bit heavyweight for your needs, but have you considered implementing XMPP protocol for your chat client?

If you do that, then your system could interoperate with Google Talk, Jabber, iChat, etc.

Alnitak
A: 

If you want the protocol to also abstract away the method invocation, have a look at XML-RPC, which Java and Python (and pretty much everything else) has good support for.

Object marshalling and unmarshalling is solid, can handle unicode, lists and dictionaries, and produces pretty human-readable output:

>>> import xmlrpclib
>>> print xmlrpclib.dumps((1, u"\xdd\xde"), methodname="my_method")
<?xml version='1.0'?>
<methodCall>
<methodName>my_method</methodName>
<params>
<param>
<value><int>1</int></value>
</param>
<param>
<value><string>ÝÞ</string></value>
</param>
</params>
</methodCall>

Basically, it has the benefits that Mapad mentions about JSON, with the extra functionality of method invocation, at the expense of (probably marginal) processing costs and (probably marginal) programming complexity.

Doug Hellman has good tutorials for both the client and server pieces of the Python XML-RPC libs here.

Alabaster Codify
Thanks for this hint, but I don't need function calls, but pure data.I think, this will be usefull later.
furtelwart