[Tutor] Ports / sockets?

Kent Johnson kent37 at tds.net
Sun Jan 30 15:42:02 CET 2005


Liam Clarke wrote:
> Hi, 
> 
> I was reading an article about 'honeytrapping' ports to slow down port
> scanners, and it made reference to Perl, but I was wondering, how
> feasible in Python is a script that waits for requests to (normally
> unused) ports, and then malforms a response to those requests?

This is similar to what a web server or any other type of network server does. Yes, you certainly 
can do this in Python.
> 
> I'm aware this is illegal in some US states, but this is more of a
> 'how would you do that' scenario rather than 'I need to do this'.
> 
> Sockets tend to use localhost? Java interpreters and Python
> interpreters all seem to call localhost.

localhost is the name of a computer, in particular the computer the program is running on. Sort of 
like 'self' in a way. To identify a port you need a host name and a port number. localhost is a host 
name.

> So, do sockets use ports? Or, do ports use sockets? Or, do programmes
> create sockets which can utilise ports? If so, can you access a port
> directly in Python?

'port' is a low-level concept that is pretty closely tied to the actual communications protocol 
(TCP/IP, for what you want to do). A socket is a programming interface to a port. Sockets use ports. 
Ports don't use sockets.

Programs create sockets either to connect directly to a port on a remote computer or to listen for 
connections. The first kind is a client socket, for example to fetch a web page from a web server 
you open up a socket to port 80 on the server. The second kind is a server socket, the web server is 
listening for clients to connect on port 80.

> 
> I always wanted to be able to the nitty gritty stuff like accessing a
> particular port, but I always thought I'd have to learn Cpp to do it.

You can definitely do this in Python.

The socket module gives you direct access to sockets. You probably don't want to use this except as 
a learning experience.

I can't find good tutorial material on the web, but some info is at
http://www.amk.ca/python/howto/sockets/

Generally you will want to program at a higher level than raw sockets. There are *lots* of Python 
libraries and frameworks to do this.

There are three fundamentally different ways to write a server. A synchronous server is the 
simplest, but it only handles one request at a time so it is not really practical. An asynchronous 
server handles multiple requests by polling the active sockets to see which ones are ready for 
service. A threaded server creates a separate thread for each active socket.

asyncore and asynchat are relatively simple libraries that let you create an asynchronous server. My 
understanding is that they are kind of limited and not really recommended for production use. 
Twisted is the king of Python asynchronous servers, if you are going to do much server writing it is 
probably worth learning it. Medusa is another robust Python async server.
http://twistedmatrix.com/
http://www.amk.ca/python/code/medusa.html
http://mail.python.org/pipermail/python-list/2003-January/137318.html

SocketServer, BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer all let you create simple 
synchonous and threaded servers.

If you are looking for HTTP servers specifically, there are lots, see here for a list:
http://www.python.org/moin/WebProgramming

> 
> Regards,
> 
> Liam Clarke



More information about the Tutor mailing list