views:

338

answers:

2

Hi, my question simply relates to the difference in performance between a socket in C and in Python. Since my Python build is CPython, I assume it's similar, but I'm curious if someone actually has "real" benchmarks, or at least an opinion that's evidence based.

My logics is as such:

  • C socket much faster? then write a C extension.
  • not/barely a difference? keep writing in Python and figure out how to obtain packet level control (scapy? dpkt?)

I'm sure someone will want to know for either context or curiosity. I plan to build a sort of proxy for myself (not for internet browsing, anonymity, etc) and will bind the application I want to use with it to a specific port. Then, all packets on said port will be queued, address header modified, and then sent, etc, etc.

Thanks in advance.

A: 

i would think C would be faster, but python would be a lot easier to manage and use.

the difference would be so small, you wouldn't need it unless you were trying to send masses amount of data (something stupid like 1 million gb/second lol)

joe

Joe Simpson
+2  A: 

In general, sockets in Python perform just fine. For example, the reference implementation of the BitTorrent tracker server is written in Python.

When doing networking operations, the speed of the network is usually the limiting factor. That is, any possible tiny difference in speed between C and Python's socket code is completely overshadowed by the fact that you're doing networking of some kind.

However, your description of what you want to do indicates that you want to inspect and modify individual IP packets. This is beyond the capabilities of Python's standard networking libraries, and is in any case a very OS-dependent operation. Rather than asking "which is faster?" you will need to first ask "is this possible?"

Greg Hewgill
Thanks Greg. You're especially right about "is this possible?" I don't believe it is. dpkt allows the ability to construct packets, but I'm confident I can't use the python socket on a packet level (against it's purpose of abstraction). So, I believe I'll have to write a C extension so that I can use the netfilter library which would allow to do such. But... this might require I do the sockets in C (I don't know, I've never done a C extension before).
Kevin
@Kevin, why are you confident you can't do that? Is the "raw" socket support not suitable? (See the last example at the end of http://docs.python.org/library/socket.html for starters.)
Peter Hansen