Python's
socket module makes it easy for you to write your own clients and server ... and yesterday I produced "Hello World" style examples of each on the final day of our
Python Programming Course. I'm going to share these examples with you here ... but I'm also going to add a word of caution first to remind you that if you're programming with a well define protocol such as ssh, http, ntp, nntp, pop3, imap, smtp, snmp, rpc, dhcp, dns, etc, then you'll do far better to use the higher level classes that someone else has already written
[http example]. Unusually, the delegates yesterday required to write the protocol themselves.
TCP v UDP
You'll read that you can program sockets using TCP/IP or using UDP ... What's the difference?
Tcp is the most common; in Tcp, the packets are synchronized so that the receiving program will get them without data loss from the sender. So that's ideal for most data interchanges - you send a database backup or a web page over a network, and you want the client to get what has been served exactly.
By contract, Udp packets are sent asynchronously, and if the client doesn't receive them from the server, that's not regarded as a catastrophic failure; you don't want too many dropped packets, but a few is no problem. You might use Udp for a telephone conversation signal, where a click on the line is far better in the event of problems than the whole system lagging to await for a retransmission.
Put it another way - Tcp is connection oriented, with the transmitter verifying the receiver and Udp is connectionless - sending the data irrespective of whether it's received at the far end.
TCP - Transmission Control Protocol
UDP - User Datagram Protocol
Client v Server
There's not as much difference as you think!
The server sits waiting for a connection from any client, whereas the client specifies which server it's to connect into and "goes for it" ... but once they're connected, they chat back and forth very much in balance.
The server needs to stay running awaiting the next connection ... the client to close down and finish.
Samples
I have added comments into each of the following, and tested them too.
TCP/IP Server in Python (test via telnet to the port)
TCP/IP Client in Python (test via our server)
UDP Server in Python (run this server ...)
UDP Client in Python (... then talk to it via this client)
(written 2010-03-25, updated 2010-03-26)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y303 - Python Network Programming [2365] Counting Words in Python via the web - (2009-08-18)
[2368] Python - fresh examples of all the fundamentals - (2009-08-20)
[2694] Multiple processes (forking) in Python - (2010-03-25)
[2765] Running operating system commands from your Python program - (2010-05-14)
[4087] Python network programming - new FTP and socket level examples - (2013-05-14)
P224 - Perl - Intersystem Communications [604] Perl - multiprocess applications - (2006-02-13)
[1073] Heartbeat script in Perl - (2007-02-09)
[1918] Perl Socket Programming Examples - (2008-12-02)
[2402] Automated Browsing in Perl - (2009-09-11)
[2876] Different perl examples - some corners I rarely explore - (2010-07-18)
[2970] Perl - doing several things at the same time - (2010-09-25)
A052 - Web Application Deployment - Networking - General [768] Our new .eu top level domain - (2006-06-19)
[2149] Routing Network Traffic - Proxies, Redirects and DNS - (2009-05-01)
[2150] Routers, Firewalls and multilayer servers - (2009-05-01)
Some other Articles
Lua tables - they are everythingRuth Davis, 1916 - 2010Email metrics and filteringGarlic bread without garlicTCP v UDP / Client v Server - Python examplesMethods that run on classes (static methods) in PythonFlexible search and replace in PythonNew brochures for the Melksham areaThe World Company Register - is it another scam?