File: Steve/Courses/2014/s2/its332/python.tex, r3463
This appendix includes example implementation of clients and servers that can exchange data across the Internet. They are implemented in Python. There is a TCP version and a UDP version of the client/server application. In addition there is an application that uses raw sockets to generate and send packets of any type.
The source code can be downloaded via http://ict.siit.tu.ac.th/~sgordon/netlab/source/.
The example application contains the server IP address (127.0.0.1), port (5005) and message (Hello World!) hardcoded into the Python source. The address used means the client and server run on the same computer (easy for testing, but not very useful). You should change them to the values appropriate for your setup.
Start the server in one terminal, and then start the client in another terminal. The client exchanges data with the server and then exits. The server remains running. The output on the server is:
The output on the client is:
Similar to the TCP Python example, the addresses and messages are hardcoded in the source for the UDP example. You should change them to values appropriate for your setup.
Start the server in one terminal, and then start the client in another terminal. The client sends a message to the server and the server returns an acknowledgement. The client waits for 0.5 seconds and then repeats. To exit the client/server press Ctrl-C.
The output on the server is:
The output on the client is:
TCP and UDP sockets provide an interface for an application to send/receive data using the respective transport protocol. In turn, both TCP and UDP use IP, which creates an IP datagram and sends it via the NIC. Raw sockets provide an interface for an application to create any type of packet and send via a chosen network interface. It provides the application direct access to send a data link layer packet (e.g. Ethernet frame), rather than having to go via TCP/IP.
Most applications don’t need raw sockets, as TCP or UDP sockets provide a much simpler interface for the service required by the application. However there are special cases when raw sockets may be used. For example, you can create packets of any format to send via a network interface for testing purposes (testing the network, testing the security of a system). Also you can capture packets of any type using raw sockets (e.g. implement your own “tcpdump”).
The following code provides an example of using raw sockets to create two types of packets:
The example Python application demonstrates how to create the two frames to be sent. The code creates the frames in their raw binary format (although using hexadecimal values instead of binary). The frames, including source/destination MAC addresses, source/destination IP addresses, packet sizes, and checksums, are hardcoded in the Python source. This wil not run on your computer: you will at least need to change the addresses and checksums. Read the source code to see suggestions on how to do this.
The application sends the two frames and then exits. To test whether it worked you should capture using tcpdump on both the sending computer and the destination computer.