Socket Programming
Socket’s are a programmer’s device for transferring bytes between programs, possibly running between different computers. Although sockets themselves transfer only byte strings, we can also transfer python objects through them by using Python’s pickle module.
Python’s socket module is just a thin wrapper (interface layer) over the underlying C library’s socket calls.
This module supports all commonly used socket types- TCP/IP, UDP, datagram and unix domain. It can be used as both a network interface API and a general IPC mechanism between processes running on the same machine.
Struct is another module that can be used to format Python objects as packed binary data byte strings for transmission, but it’s scope is limited.
Besides data communication, the socket module also supports the following tasks.
- Converting bytes to a standard network ordering (ntohl, htonl)
- Querying machine name and address (gethostname, gethostbyname)
- Wrapping socket objects in a file object interface (sockobj.makefile)
- Making socket calls nonblocking (sockobj.setblocking)
- Setting socket timeouts (sockobj.settimeout)