Networking programming
The programming is heavily infleuced by the TCP specification
and the Berkeley socket programming interface.
The Python socket
interface mimics the classic interface.
Look at a 2011 course page for more information.
Echo
There’s a server for the echo protocol running on port 7 on uncacsci-pi3-v.cs.unca.edu.
Try to connect to it using telnet (yes, it still exists) and netcat (the network geek’s back-end) tool.
Client Python programming
Do it 0
Look at the Python class socket
interface.
Using Python interactively connect to the echo server.
You’ll need to create the socket, connect to the echo server,
send a message, read a message, and close the socket!
Do it 1
Once you get that working, create an executable Python script to echo using a remote computer. (Yep, it’s silly.)
Do it 2
Modify your program to send your input line in chunks.
For example, send "Hello "
and then
five seconds later (use time.sleep()
) send
"World\r\n"
.
Do you need two calls to recv
or just one?
Do it 3
I generally use for make separate read and write files for the
read and write ends of the socket.
See another
old class page for an example.
Doing this allows you to use the
Python file objects methods.
This can be very useful for reading and writing the whole line
using readline
and writeline
.
A server
Writing a server with the socket
interface isn’t
easy. Take a look at an
an
old example (which can only handle one client at a time).
Take a look at the SocketServer interface and try to create your own echo server.
For now don’t worry about handling multiple connections.