Network Programming in Python based on Chun, chapter 2; plus material on classes What is Network Programming? Writing programs that communicate with other programs Communicating programs typically on different computers - but can be on the same computer Running programs are known as processes Processes connect to each other through sockets 1
Network Programs and Sockets A network program needs to be identified - What host computer is it on? - Which process on that computer it? Host computers identified by IP address (Networking) processes on a computer identified by port number - e.g. standard web server - always port 80 Combination of a port and an IP address forms a socket - Networking processes uniquely identified by socket Processes communicate through their sockets Client Processes, Server Processes client process server process client process client process sockets client process 2
Client/Server Architecture Basic model for communicating processes Server passively listens for, accepts communication requests - establishes listening socket Clients actively send requests - use "ephemeral" (temporary) sockets Communications between client, server can be one-way or two-way Connection-Oriented Sockets Server creates a "stream socket" based on a publicly announced port number Client (automatically) chooses an ephemeral port number, creates its own socket - uses socket to request connection with server's socket Once connection is made, both sides can send and receive a continual stream of bytes 3
Connectionless Sockets Server listens on a "datagram socket" again using a publicly announced port Client sends groups of data bytes as distinct "datagrams" to server's socket - no attempt to connect to server socket first, so no certainty that the datagram will be received by anything Server can optionally send a response datagram back - again with no certainty that the response will be received Sockets and Python "import socket" brings in the socket.socket class Basic usage: - newsocket = socket.socket(family, type) Socket family needs to be "AF_INET" for Internet usage Socket type is "SOCK_STREAM" or "SOCK_DGRAM" - Selects connection-oriented, i.e. TCP, or connectionless, i.e. UDP 4
Steps For Using a Server Socket Create socket Bind socket to an (IP-addr, port-#) pair Wait for connections - Stream / TCP :» listen for connection request» accept request and create communication socket - Dgram / UDP:» receive datagram containing data bytes Send, receive data as appropriate - Use send() or sendall(), and recv() UDP Communications UDP server merely listens on its socket for incoming UDP datagrams UDP client sends messages from its socket "blindly", with no knowledge of whether the server is listening or not Server may optionally respond by sending another datagram back to the client's IP-address/port as specified in the received message 5
A UDP Client A UDP Client - B 6
A UDP Server exercise Modify the UDP server to accept (and ignore) whatever the UDP client sends, and return the current time and date. Modify the UDP server and client to act as "chat" applications each process gets keyboard input and sends it to the other; receives data from the other and displays it on the console. 7
TCP Sessions TCP server listens on its socket for an incoming connection request - Ordinarily, will accept an incoming request - Produces secondary socket for actual communications TCP client first attempts to connect to the server's socket Once connection is established, data can be sent and received in both directions Example: A TCP Socket (part A) 8
Example: A TCP Socket (part B) Example: A TCP Socket (part C) 9
Example: A TCP Socket (part D) A TCP Client (part A) 10
A TCP Client (part B) A TCP Client (part C) 11
exercises Modify the TCP server to add a timestamp to the client's data and return that as a response. Have the server save everything the client sends to a local text file, until the client sends a blank (empty) line. - Do "blank" lines have a newline at the end? Have the server send a local text file back to the client. - Let the client specify which file? Use the getpass module in the client, to input a password without showing it. 12