LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 08-30-2004, 09:12 PM   #1
Ikana
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, 08:35 PM   #2
irfanhab
Member
 
Registered: Jan 2004
Location: Pakistan
Distribution: OpenSuse 10.2, Slackware 11, Solaris 10
Posts: 415

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

Code:
tcpSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Likewise, to create a UDP/IP socket you perform:

Code:
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.,

Code:
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:

Code:
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.recv()/cs.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 08:38 PM.
 
  


Reply


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 06:27 AM
ping vs traceroute shesundar Linux - Networking 1 11-30-2005 04:33 AM
users can't use ping traceroute ... kvtournh Mandriva 1 11-16-2004 06:43 AM
network question: traceroute pdelucia Linux - Networking 21 10-09-2004 06:30 PM
Unable to ping or Traceroute or FTP from local network. retheesh Linux - Networking 3 10-15-2003 06:20 AM


All times are GMT -5. The time now is 02:29 PM.

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