fail to connect to the web server using telnet module

printf printf at hongkong.com
Sat May 24 11:15:34 EDT 2003


I try to use my python script to connect to my apache web server. The 
web server required a login password, but it fails to do so.

------The web server's response begin ------------
HTTP/1.1 401 Authorization Required
Date: Sat, 24 May 2003 14:56:38 GMT
Server: Apache-AdvancedExtranetServer/1.3.20 (Mandrake Linux/3mdk) 
mod_perl/1.25_01 PHP/4.0.6
WWW-Authenticate: Basic realm="dataDirectory"
Connection: close
Content-Type: text/html; charset=iso-8859-1
...
...
------The web server's response end ------------


So, I suspect my password is wrong. Then I capture the data from my 
browser to the web server to verify the encode password, it's same anyway.

----the data I capture begin------
GET /~derek/ HTTP/1.1
Connection: Keep-Alive
User-Agent: Mozilla/5.0 (compatible; Konqueror/2.2.1; Linux)
Accept: text/*, image/jpeg, image/png, image/*, */*
Accept-Encoding: x-gzip, gzip, identity
Accept-Charset: iso-8859-15, utf-8, *
Accept-Language: en_US, en
Host: localhost
Authorization: Basic ZGVyZWs6ZGVyZWs=
----the data I capture end------


I'm not sure what it is missing here.

My code is
---------------------------
#! /usr/bin/env python

import telnetlib
import sys

HOST="localhost"
PORT=80
GetString="GET http://localhost/~derek/ HTTP/1.0"
HeadString="HEAD / HTTP/1.0"


def auth(_user, _passwd):
	import crypt,base64

	salt="iZ"
	cryptPasswd = crypt.crypt(_passwd, salt)
	encodeString = base64.encodestring(_user+":"+cryptPasswd)
	if encodeString[-1]=='\n':
		encodeString = encodeString[0:-1]
	return encodeString


authString= auth("derek","derek")
print authString
#sys.exit(-1)

telnet = telnetlib.Telnet(HOST, PORT)


telnet.write("GET /~derek/ HTTP/1.0\r\n")
telnet.write("Connection: Keep-Alive\r\n")
telnet.write("User-Agent: Mozilla/5.0 (compatible; Konqueror/2.2.1; 
Linux\r\n")
telnet.write("Accept: text/*, image/jpeg, image/png, image/*, */*\r\n")
telnet.write("Accept-Charset: iso-8859-15, utf-8, *\r\n")
telnet.write("Accept-Language: en_US, en\r\n")
telnet.write("Host: localhost\r\n");
telnet.write("\r\n")

#print telnet.read_all()
telnet.write("GET /~derek/ HTTP/1.0\r\n")
telnet.write("Connection: Keep-Alive\r\n")
telnet.write("User-Agent: Mozilla/5.0 (compatible; Konqueror/2.2.1; 
Linux\r\n")
telnet.write("Accept: text/*, image/jpeg, image/png, image/*, */*\r\n")
telnet.write("Accept-Charset: iso-8859-15, utf-8, *\r\n")
telnet.write("Accept-Language: en_US, en\r\n")
telnet.write("Host: localhost\r\n");
telnet.write("Authorization: Basic ZGVyZWs6ZGVyZWs=")
telnet.write("\r\n\r\n")
print telnet.read_all()






More information about the Python-list mailing list