Logo F2FInterview

Python Advanced Interview Questions

Q   |   QA

Now we get to communicate with the server. In retrieving information from a web server, web browsers send their requests in the following format:

<type of request> <file name> <protocol to use>

Our web client will ask the server to "GET" a given file using the HTTP 1.0 protocol. To communcate this, we write to the file-like object of the socket instance:

fileobj.write("GET "+filename+" HTTP/1.0\n\n")

The server will then respond with the data, and that data will be buffered as a file-like object. But our program will not get it unless we read it. So read the file object into another variable, here called buff.

buff = fileobj.readlines()

Now, all of the data is contained within buff. All we need to do is step through it, printing as we go.

for line in buff: print line

Now, you can save the program and call it with the name of the server and the name of the file you want to see. For example, try:

python simple_web_client.py f2finterview.com /

If you run your own server, you can access your web directory as follows:

python simple_web_client.py localhost / What you will receive is the raw HTML of the index file. You can then process it as plain text or parse it with urllib or urllib2

To ensure that you have all the lines needed for this program, here is the code for this web client.

#!/usr/bin/env python

# import sys for handling command line argument
# import socket for network communications
import sys, socket

# hard-wire the port number for safety's sake
# then take the names of the host and file from the command line
port = 80
host = sys.argv[1]
filename = sys.argv[2]

# create a socket object called 'c'
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# connect to the socket
c.connect((host, port))

# create a file-like object to read
fileobj = c.makefile('r', 0)

# Ask the server for the file
fileobj.write("GET "+filename+" HTTP/1.0\n\n")

# read the lines of the file object into a buffer, buff
buff = fileobj.readlines()

# step through the buffer, printing each line
for line in buff:
print line

As a complement to the network client tutorial, this tutorial shows how to implement a simple web server in Python. To be sure, this is no substitute for Apache or Zope. There are also more robust ways to implement web services in Python, using modules like BaseHTTPServer. This server uses the socket module exclusively.

You will recall that the socket module is the backbone of most Python web service modules. As with the simple network client, building a server with it illustrates the basics of web services in Python transparently. BaseHTTPServer itself imports the socket module to affect a server.

By way of review, All network transactions happen between clients and servers. In most protocols , the clients ask a certain address and receive data.

Within each address, a multitude of servers can run. The limit is in the hardware. With sufficient hardware (RAM, processor speed, etc.), the same computer can serve as a web server, an ftp server, and mail server (pop, smtp, imap, or all of the above) all at the same time. Each service is associeted with a port. The port is bound to a socket. The server listens to its associated port and gives information when requests are received on that port.

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: