PyNewbie on Using Sockets in Python
A socket is a connection from one program to another. The two programs are typically on different machines, but they can also be on the same machine. Most sockets are from a client program connect to a server program to get some sort of information. Most socket transmissions are bi-directional -- send a request, receive some information. When you request a web page from within a web browser, you are causing the initiation of a socket connect from your computer (the client) to the web server's computer (the server, which could also be located on the same machine). You request a particular web page and the server responds with the information which the web browser displays as the web page.
To create a connection in Python, one imports the built-in socket module, creates a sockect object, and calls the methods to establish a connection, send and receive data, etc. To import the socket module and create a socket object looks like this:
from socket import *
sockobj = socket(AF_INET, SOCK_STREAM)
The first line imports the socket module. The next line creates the socket object. Using the 'AF_INET' and 'SOCK_STREAM' together tells the socket module to create a TCP/IP socket.
The next step would be to connect to a server and send or receive information...
sockobj.connect(("128.59.16.20", 13))
sockobj.send("Python User")
data = sockobj.recv(1024)
In this segment, we connect to a time server at IP address 128.59.16.20 on port 13, the typical 'daytime' service port. We sent a name to the server for identification purposes only using the send method. Some time servers require information sent and some don't, while some will only allow certain clients to connect to them. Finally, we received the time data from the server.
The data is small and we didn't need to create a loop to receive data larger than the 1024 bytes. sockobj.recv(1024) reads at most 1024 bytes of data and returns it as a string. If the data is longer than 1024 bytes a loop is required to get the rest of the data. When no more data is available, an empty string is returned signifying the end of the data stream.
I supposed you'd like to see the response, so we'll print what we got.
print 'response was:', data
sockobj.close()
Here we simply printed the data and closed the socket connection.
The complete script is the following:
from socket import *
sockobj = socket(AF_INET, SOCK_STREAM)
sockobj.connect(("128.59.16.20", 13))
sockobj.send("Python User")
data = sockobj.recv(1024)
print 'response was:', data
sockobj.close()
The result I got when I ran the following was:
# python pysocket.py
response was: Mon Jul 14 02:02:56 2003
That concludes my initial small Python scripting lesson. It was only an exercise for me to learn more about Python. If motivated (or I get feedback), I hope to write more as I learn more about Python.
About this entry
Date Posted:
July 13th 2003 at 12:07:00 PM
Tagged:
python
Next Entry:
PyNewbie on making cryptograms
Comments
blog comments powered by Disqus