Python_Network_Programming.pdf

(308 KB) Pobierz
92254282 UNPDF
Python
Network Programming
by Sebastian V. Tiponut
Technical University Timisoara
Version 0.00, 16. July 2001
92254282.012.png
2
LIST OF FIGURES
Contents
1 Introduction
4
2 Basic socket usage 5
2.1 Creating a socket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Connecting a socket and data transfer . . . . . . . . . . . . . . . . . . . . . . . 6
2.3 Binding a name to socket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.4 Listening and accepting connections . . . . . . . . . . . . . . . . . . . . . . . . 6
2.5 UDP sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.6 Closing the socket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.7 Using functions provided in socket module . . . . . . . . . . . . . . . . . . . . . 8
2.7.1 Functions based on resolver library . . . . . . . . . . . . . . . . . . . . . 8
2.7.2 Service-related functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.7.3 Miscellaneous functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3 Basic network structures design 9
3.1 Designing a TCP server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2 The TCP client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.3 Modeling datagram applications . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4 Advanced topics on servers 13
4.1 Building a pristine environment . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.2 Handling multiple connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.2.1 Threaded servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.2.2 Using select . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.2.3 Fork servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.3 Dealing with classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.3.1 Simple connection object . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.3.2 Applying a design pattern . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.4 Advanced aspects concerning clients . . . . . . . . . . . . . . . . . . . . . . . . 22
5 HTTP protocol 23
5.1 CGI module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
5.1.1 Build a simple CGI script . . . . . . . . . . . . . . . . . . . . . . . . . . 23
5.1.2 Using CGI module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
5.1.3 Conguring Apache on Linux for using with CGI scripts . . . . . . . . . 25
6 Common protocols 26
6.1 Designing Telnet applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
6.2 File Transfer Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
6.3 SMTP protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
7 TO DOs
30
List of Figures
1
TCP connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2
UDP connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
92254282.013.png 92254282.014.png
LIST OF FIGURES
3
3
Threaded server diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4
Fork server diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5
Designing a TCP connection with state pattern . . . . . . . . . . . . . . . . . . 20
92254282.015.png
4
1 INTRODUCTION
1 Introduction
Network programming is a buzzword now in the soft world. We see the market lled with
an avalanche of network oriented applications like database servers, games, Java servlets and
applets, CGI scripts, dierent clients for any imaginable protocol and the examples may con-
tinue. Today, more then half of the applications that hit the market are network oriented.
Data communication between two machines (on local net or Internet) is not any more a cu-
riosity but is a day to day reality. \The network is the computer" says the Sun Microsystem's
motto and they are right. The computer is no more seen as a separate entity, dialogging only
with it's human operator but as part of a larger system - the network, bound via data links
with other thousands of other machines.
This paper is presenting a possible way of designing network-oriented applications using
Python. Because the author is a Linux fan, the examples contained in this paper are related
to Linux 1 and apologizes all the Windows or Mac OS users (fans ?) for any inconvenience
on reading this text. With a little eort, the examples are portable to another non-UNIX
operation system. Presenting a quick structure of this paper, rst four sections are dealing
with primitive design { at socket level { of network applications. The remaining sections are
treating specic protocols like http, ftp, telnet or smtp. The section dealing with http will
contain a subsection about writing CGI scripts and using the cgi module.
Going further on more concrete subjects, we are going to analyze the possibilities of
network programming provided in Python. Raw network support is implemented in Python
through the socket module, this module comprising mostly of the system-calls, functions and
constants dened by the 4.3BSD Interprocess Communication facilities (see [1]), implemented
in object-oriented style. Python oers a simple interface (much simpler than the corresponding
C implementation, though based on this one) to properly create and use a socket. Primarily,
is dened the socket() function returning a socket object 2 . The socket has several methods,
corresponding to their pairs from C sys/socket.h , like bind() , connect() , listen() or
accept() . Programmers accustomed with socket usage under C language 3 will nd very easy
to translate their knowledge in the more-easy-to-use socket implementation under Python.
Python eliminates the daunting task of lling structures like sockaddr in or hostent and ease
the use of previously mentioned methods or functions { parameter passing and functions call
are easier to handle. Some network-oriented functions are provided too: gethostbyname() ,
getprotobyname() or conversion functions ntohl() , htons() , useful when converting integers
to and from network format. The module provides constants like SOMAXCONN , INADDR * , used
in gesockopt() or setsockopt() functions. For a complete list of above mentioned constants
check your UNIX documentation on socket implementation.
Python provide beside socket , additional modules (in fact there is a whole bundle of them)
supporting the most common network protocols at user level. For example we may nd useful
modules like httplib , ftplib , telnetlib , smtplib . There is implemented support for CGI
scripting through cgi module, a module for URL parsing, classes describing web servers
and the examples may continue. This modules are specic implementations of well known
protocols, the user being encouraged to use them and not trying to reinvent the wheel. The
author hopes that the user will enjoy the richness of Python's network programming facilities
and use them in new and more exciting ways.
Because all the examples below are written in Python, the reader is expected to be uent
with this programming language.
1 And to other *NIX systems, POSIX compliant.
2 We call this further just socket.
3 4.3BSD IPC implementation found on mostly UNIX avors.
92254282.001.png 92254282.002.png 92254282.003.png 92254282.004.png
2 BASIC SOCKET USAGE
5
2 Basic socket usage
The socket is the basic structure for communication between processes. A socket is dened
as \an endpoint of communication to which a name may be bound" [1]. The 4.3BSD imple-
mentation dene three communication domains for a socket: the UNIX domain for on-system
communication between processes; the Internet domain for processes communicating over
TCP(UDP)/IP protocol; the NS domain used by processes communicating over the old Xe-
rox communication protocol.
Python is using only 4 the rst two communication domains: UNIX and Internet domains,
the AF UNIX and AF INET address families respectively. UNIX domain addresses are repre-
sented as strings, naming a local path: for example /tmp/sock . This can be a socket created
by a local process or, possibly, created by a foreign process. The Internet domain addresses
are represented as a (host, port) tuple, where host is a string representing a valid Internet
hostname, say matrix.ee.utt.ro or an IP address in dotted decimal notation and port is
a valid port between 1 and 65535 5 . Is useful to make a remark here: instead of a qualied
hostname or a valid IP address, two special forms are provided: an empty string is used
instead INADDR ANY and the '<broadcast>' string instead of INADDR BROADCAST .
Python oer all ve type of sockets dened in 4.3BSD IPC implementation. Two seem
to be generally used in the vastness majority of the new applications. A stream socket is a
connection-oriented socket, and has the underlaying communication support the TCP proto-
col, providing bidirectional, reliable, sequenced and unduplicated ow of data. A datagram
socket is a connectionless communication socket, supported through the UDP protocol. It
oers a bidirectional data ow, without being reliable, sequenced or unduplicated. A process
receiving a sequence of datagrams may nd duplicated messages or, possibly, in another order
in which the packets were sent. The raw, sequenced and reliably delivered message sockets
types are rarely used. Raw socket type is needed when one application may require access to
the most intimate resources provided by the socket implementation. Our document is focusing
on stream and datagram sockets.
2.1 Creating a socket
A socket is created through the socket(family, type[, proto]) call; family is one of the
above mentioned address families: AF UNIX and AF INET , type is represented through the fol-
lowing constants: SOCK STREAM, SOCK DGRAM, SOCK RAW, SOCK SEQPACKET and SOCK RDM .
proto argument is optional and defaults to 0. We see that socket() function returns a socket
in the specied domain with the specied type. Because the constants mentioned above are
contained in the socket module, all of them must be used with the socket.CONSTANT nota-
tion. Without doing so, the interpreter will generate an error. To create a stream socket in
the Internet domain we are using the following line:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Substituting socket.SOCK_STREAM with socket.SOCK_DGRAM we create a datagram socket in
the Internet domain. The following call will create a stream socket in the UNIX domain:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
We discussed so far about obtaining a socket of dierent types in dierent communication
domains.
4 Xerox Network System is no longer used.
5 Using a port under 1000 must be done with root privileges.
92254282.005.png 92254282.006.png 92254282.007.png 92254282.008.png 92254282.009.png 92254282.010.png 92254282.011.png
Zgłoś jeśli naruszono regulamin