[Tutor] proxy requiring authentication

Christopher Smith csmith@blakeschool.org
Thu Apr 10 11:22:03 2003


I have read on in the doc's that proxies that require authentication are
not supported by urllib's functionality.  I also read a PEP that said that
this is in process for a future release.  In the meantime, is it easy to
explain why one can't just open a socket through python and print the
required commands to the proxy?  Here, for example, is how someone did it
in Java:

#class S
#{
#    public static void main(String args[])
#    {
#        try {
#            Socket s = new Socket("proxy.server.name", 80);
#        
#            OutputStream os = s.getOutputStream();
#            PrintWriter pw = new PrintWriter(os);
#
#            InputStream is = s.getInputStream();
#            InputStreamReader isr = new InputStreamReader(is);
#            BufferedReader br = new BufferedReader(isr);
#
#                //send request
#            pw.println("GET http://www.google.com HTTP/1.1");
#            pw.println("Proxy-Authorization: Basic "+
#                    Base64.encodeString("username:password"));
#            pw.println(""); // this blank line completes the request
#            pw.flush();
#
#                //get reply
#            String str = br.readLine();
#            while (str != null)
#            {
#                System.out.println(str);
#                str = br.readLine();
#            }
#        } catch (Exception e) {
#                System.err.println(e);
#        }
#    }//main()
#
#}

Can this be translated into Python?  I don't know much about working with
sockets but read the docs and attempted the following without success (I
only show the first few lines because the connection could not even be
established):

###
import socket
theproxyserver='the.proxy.server' #but I used our proxy name
theproxyport=42 #but I used the real port number
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((theproxyserver, theproxyport)) #fails with error (49, can't assign
address)
###

Thanks for any help.

/c