I am trying to learn network programming in Python and I've just about Googled the topic to death without much in the way of results.
Don't Worry LinuxQuestions to the Rescue!
Safari | Core Python Programming -> Network Programming in Python
Network Programming in Python
Now that you know all about client-server architecture, sockets, and networking, let us try to bring this concept to Python. The primary module we will be using in this section is the socket module. Found within this module is the socket() function, which is used to create socket objects. Sockets also have their own set of methods which enable socket-based network communication. socket() Module Function To create a socket, you must use the socket.socket() function, which has the general syntax:
socket (socket_family, socket_type, protocol=0)
The socket_family is either AF_UNIX or AF_INET, as explained earlier, and the socket_type is either SOCK_STREAM or SOCK_DGRAM, also explained earlier. The protocol is usually left out, defaulting to 0. So to create a TCP/IP socket, you call socket.socket() like this:
tcpSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Likewise, to create a UDP/IP socket you perform:
udpSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
Since there are numerous socket module attributes, this is one of the exceptions where using "from module import *" is acceptable because of the number of module attributes. If we applied "from socket import *", we bring the socket attributes into our namespace, but our code is shortened considerably, i.e.,
tcpSock = socket(AF_INET, SOCK_STREAM)
Once we have a socket object, all further interaction will occur using that socket object's methods. Socket Object (Built-in) Methods In Table 16.1, we present a list of the most common socket methods. In the next subsection, we will create both TCP and UDP clients and servers, all of which use these methods. Although we are focusing on Internet sockets, these methods have similar meanings when using Unix sockets.
Table 16.1. ,
Method Server Socket Methods Common Socket Object Methods
s.bind() bind address (hostname, port number pair) to socket
s.listen() set up and start TCP listener passively accept TCP client connection
s.accept() waiting until connection arrives (blocking)
Client Socket Methods
s.connect() actively initiate TCP server connection
General Socket Methods
s.recv() receive TCP message
s.send() transmit TCP message
s.recvfrom() receive UDP message
s.sendto() transmit UDP message
s.close() close socket
Creating a TCP Server We will first present some general pseudocode involved with creating a generic TCP server, then describe in general what is going on. Keep in mind that this is only one way of designing your server. Once you become comfortable with server design, you will be able to modify the pseudocode to operate the way you want it to:
ss = socket() #create server socket
ss.bind() #bind socket to address
ss.listen() # listen for connections
inf_loop: #server infinite loop
cs = ss.accept() # accept client connection
comm_loop: #communication loop dialog (receive/send)
cs.close() # close client socket
ss.close() #close server socket
All sockets are created using the socket.socket() function. Servers need to "sit on a port" and wait for requests, so they all must "bind" to a local address. Because TCP is a connection-oriented communication system, some infrastructure must be set up before a TCP server can begin operation. In particular, TCP servers must "listen" for (incoming)
connections. Once this setup process is complete, a server can start its infinite loop. A simple (single-threaded) server will then sit on an accept() call waiting for a connection. By default, accept() is blocking, meaning that execution is suspended until a connection arrives. Sockets do support a non-blocking mode; refer to the documentation or operating systems textbooks for more details on why and how you would use non-blocking sockets. Once a connection is accepted, a separate client socket is returned [by accept()] for the upcoming message interchange. Using the new client socket is similar to handing off a customer call to a service representative. When a client eventually does come in, the main switchboard operator takes the incoming call and patches it through, using another line to the right person to handle their needs. This frees up the main line, i.e., the original server socket so that the operator can resume waiting for new calls (client requests) while the customer and the service representative he or she was connected to carry on their own conversation. Likewise, when an incoming request arrives, a new communication port is created to converse directly with that client while the main one is free to accept new client connections. NOTE We do not implement this in our examples, but it is also fairly common to hand a client request off to a thread or new process to complete the client processing. The SocketServer module, a high-level socket communication module written on top of socket, supports both threaded and spawned process handling of client requests. We refer the reader to the documentation to obtain more information about the SocketServer module as well as the exercises in Chapter 17, Multithreaded Programming.
Once the temporary socket is created, communication can commence, and both client and server proceed to engage in a dialog of sending and receiving using this new socket until the connection is terminated. This usually happens when one of the parties either closes its connection or sends an empty string to its partner. In our code, after a client connection is closed, the server goes back to wait for another client connection. The final line of code, where we close the server socket, is never encountered since it is supposed to run in an infinite loop. We leave this code in our example as a reminder to the reader that calling the close() method is recommended when implementing an intelligent exit scheme for the server, for example, a handler which detects some external condition whereby the server should be shut down. In those cases, a close() method call is warranted.
Now I think you would have some idea how to do network programming in Python
Anymore examples and stuff just ask in LQ!