Logo F2FInterview

Python Advanced Interview Questions

Q   |   QA

All network transactions happen between clients and servers. In most protocols , the clients ask a certain address and receive data. The servers watch a port and give information.

To affect a network connection you need to know the host, the port, and the actions allowed on that port. Each port is associated with a service. Each server watches a different port.

Most web servers run on port 80, though it may sometimes be 8080. FTP lives on port 21, and secure shell (SSH) is on 22. For email, POP, SMTP, and IMAP all live on different ports.

You should note that these addresses are the common port numbers for the different services. A network administrator can change them for his or her network. As long as the client asks for the correct service on the right port at the right address, communication will still happen. Google's mail service, for example, does not run on the common port numbers but, because they know how to access their accounts, users can still get their mail.

As usual, our program will be more flexible if we assign values dynamically instead of hard-coding values. Therefore, let's import the sys module so we can grab input from the command line.

Next, import the socket module. This is the bedrock of most network programming in Python. While different modules exist for the various protocols, the socket module allows you to access any port on any machine and read or write to it. Other modules are certainly more appropriate for their given tasks (e.g., httplib, ftplib, gopher, poplib, etc.), but socket is foundational to each of the others (the httplib module, for example, imports socket).

Next, we need to declare a few variables.

As I mentioned earlier, every network client needs to know the address of the machine, the port of the service, and the name of the file on which it is to operate. Theoretically, we could take the port number from the command line. However, because the port usually determines the service, it is safer to hardwire the port (and therefore the service) into the program. For a web service, we will look on port 80.

import port = 80

Next, let's take the server's address and the name of the file from the command line. Because we are working on the Web, we can make us of the DNS and allow for URLs.

host = sys.argv[1]
filename = sys.argv[2] 

In order to access the Internet, we need to create a socket. The syntax for this call is as follows:

<variable> = socket.socket(<family>, <type>) </blockquote>

The recognised socket families are: 

  • AF_INET: IPv4 protocols (both TCP and UDP)
  • AF_INET6: IPv6 protocols (both TCP and UDP)
  • AF_UNIX: UNIX domain protocols 

The first two are obviously internet protocols. Anything that travels over the internet can be accessed in these families. Many networks still do not run on IPv6. So, unless you know otherwise, it is safest to default to IPv4 and use AF_INET.

The socket type refers to the type of communication used through the socket. The five socket types are as follows:

  • SOCK_STREAM: a connection-oriented, TCP byte stream
  • SOCK_DGRAM: UDP transferral of datagrams (self-contained IP packets that do not rely on client-server confirmation)
  • SOCK_RAW: a raw socket
  • SOCK_RDM: for reliable datagrams
  • SOCK_SEQPACKET: sequential transfer of records over a connection

By far, the most common types are SOCK_STEAM and SOCK_DGRAM because they function on the two protocols of the IP suite. The latter three are much rarer and so may not always be supported.

Let's therefore create a socket and assign it to variable; here I use c (for connection).

c = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

After creating the socket, we need to connect to it using the connect method of the socket object. The socket is essentially an opening in the networking capacity of the computer. In connecting, we give it a host and port for network communication.

c.connect((host, port))

It is worth noting that the address for the connection is given as a tuple (hence the double parentheses).
Before we can read from the socket, however, we need to make a file-like object from it. Remember, Python read and writes to file-like objects, not sockets. So, we essentially tell Python to view the socket as a file. All sockets have a function makefile that takes two arguments: the mode and the buffer size. In our case, we want a simple read mode that has no buffer.

fileobj = c.makefile('r', 0)

In order to link this F2FInterview's page as Reference on your website or Blog, click on below text area and pres (CTRL-C) to copy the code in clipboard or right click then copy the following lines after that paste into your website or Blog.

Get Reference Link To This Page: (copy below code by (CTRL-C) and paste into your website or Blog)
HTML Rendering of above code: