Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C/Python Socket Performance?

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.

like image 937
Kevin Avatar asked Dec 15 '09 18:12

Kevin


2 Answers

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?"

like image 111
Greg Hewgill Avatar answered Oct 16 '22 07:10

Greg Hewgill


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

like image 26
Joe Simpson Avatar answered Oct 16 '22 08:10

Joe Simpson