Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-30-2004, 10:12 PM   #1
LQ Newbie
Registered: Aug 2004
Distribution: FreeBSD 4.10, Fedora Core 2
Posts: 11

Rep: Reputation: 0
network programming (esp. ping/traceroute) in Python

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. So I'd like to know if anyone here has any references, guides or tutorials to share concerning network programming in Python.

I'm guessing it will be done by using sockets to some degree.I would appreciate it very much if someone would point me in the right direction.
Old 08-31-2004, 09:35 PM   #2
Registered: Jan 2004
Location: Pakistan
Distribution: OpenSuse 10.2, Slackware 11, Solaris 10
Posts: 415

Rep: Reputation: 34
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!

Last edited by irfanhab; 08-31-2004 at 09:38 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Want to log traceroute or ping times wkm001 Linux - Software 4 10-22-2011 07:27 AM
ping vs traceroute shesundar Linux - Networking 1 11-30-2005 05:33 AM
users can't use ping traceroute ... kvtournh Mandriva 1 11-16-2004 07:43 AM
network question: traceroute pdelucia Linux - Networking 21 10-09-2004 07:30 PM
Unable to ping or Traceroute or FTP from local network. retheesh Linux - Networking 3 10-15-2003 07:20 AM

All times are GMT -5. The time now is 11:28 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration