Python Tricks

Network Programming

Socket Programming HOWTO:

socket — Low-level networking interface:

Creating a Socket

Create an INET, STREAM[ing] socket. And connect to the web server on port 80 - the typical http port.

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 80))

Creating a server socket:

import socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Use socket.gethostname() instead of localhost to connect externally.
serversocket.bind(('localhost', 80))
serversocket.listen(5) # Accept 5 simultaneous incoming connections.


select – Wait for I/O Efficiently: