[Tutor] Passing HTTP commands through Telnet using Python for web site testing?

Kent Johnson kent37 at tds.net
Thu Nov 17 12:13:49 CET 2005

Adisegna wrote:
> Hi,
> I just started learning Python and would like to try writing a 
> monitoring script.
> What I would like to do is use Telnet to send a GET via port 80 to a 
> status page in a directory to verify my site is up.

I use httplib for this. You can probably do it with urllib2 also but I don't know how to get the status codes from urllib2.

Here is an incomplete snippet:

            if self.method == 'http':
                conn = HTTPConnection(self.server)
                conn = HTTPSConnection(self.server)
            conn.request('GET', self.path)

            resp = conn.getresponse()
            data = resp.read()
            status = resp.status

            if status == 200:
                success = self.successText in data
        except socket.timeout:
            clientErrorMsg = 'Ping failed due to timeout (%s seconds)' % timeout
        except socket.error, msg:
            clientErrorMsg = 'Ping failed with error ' + str(msg)
            clientErrorMsg = ''.join(traceback.format_exception(*sys.exc_info()))



More information about the Tutor mailing list