A simple socket demo

The goal of this project is to introduce you to the basics of network programming and how to use it in your GUI application. 

Python has great support for network programming. At the lowest level, Python provides a socket module that lets you connect and interact with the network using a simple-to-use, object-oriented interface.

For those new to socket programming, sockets are the fundamental concept behind any kind of network communication done by your computer. For instance, when you type www.packtpub.com in your browser, the operating system on your computer opens a socket and connects to the remote server to fetch the web page for you. The same happens with any application that needs to connect to the network.

More specifically, sockets refer to a communications endpoint that is characterized by a five-element tuple that contains the following information: 

(protocol, local address, local port, remote address, remote port) 

This tuple must be unique for communication on a channel between a local machine and a remote machine.

Sockets may be connection-oriented or connectionless. Connection-oriented sockets allow for the flow of data to and fro as required. Connectionless sockets (or datagram sockets) allow only one message at a time to be transmitted, without an open connection.

Sockets can be classified into different types or families. The two most common socket families are AF_INET (for internet connections) and AF_UNIX for interprocess communications on a Unix machine. We will use AF_INET in our chat program.

This is the lowest level at which a programmer can access the network. Underneath the socket layer lie raw UDP and TCP connections, which are handled by your computer's operating system with no direct access points for programmers.

Let's take a brief look at some of the APIs available in the socket module:

API  Description
socket.socket
(addressfamily=AF_INET,
type=SOCK_STREAM, proto=0,
fileno=None)
Creates a socket. The addressfamily represents the format for providing the address, normally the IP address; type is usually SOCK_STREAM for TCP or SOCK_DGRAM for the UDP connection protocol. The protocol number is usually zero and may be omitted. Returns a socket object.
socket.bind(address) Associates a local address with a socket. The socket must not already be bound. (The format of the address depends on the address family defined when creating the socket.)
socket.listen(backlog) Announces a willingness to accept connections. The backlog argument specifies the maximum number of queued connections and should be at least zero; the maximum value is system-dependent.
socket.accept() Passively establishes an incoming connection. Before accepting, the socket must be bound to an address and listening for connections. Returns a (conn, address) pair, where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection.
socket.connect() Actively attempts to establish a connection to a remote socket at the address.
socket.send(bytes)/socket.
sendall(bytes)
Sends some data over the connection. Unlike send(), sendall(), this continues to send data from bytes until either all data has been sent or an error occurs. Returns None on success.
socket.recv(bufsize) Receives some data over the connection. Returns a bytes object representing the data received. The maximum amount of data to be received at once is specified by bufsize.
socket.close() Releases the connection. The underlying system resource (for example, a file descriptor) is also closed.

 

If you look at the 9.07_socket_demo.py Python file in the code bundle of this project, you'll find that it sends a very obscure-looking GET request to fetch the contents from the URL in the following line of code:

message = "GET / HTTP/1.1 
Host:" + host + "

Accept: text/html

"

The data received from the server is also sent in packets, and it is our task to collect all the data and assemble it at our end.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.119.81