1
votes

I'm trying to set up a proxy and I'm using the socket and httlib modules. I have my web browser pointed to the localhost and port on which the server is running,and I'm handling HTTP requests through the server. I extract the url from the HTTP header when the browser is requesting a web page and then trying to make a request through the proxy in the following manner:-

conn = httplib.HTTPSConnection(url,serverPort)
conn.request("GET",url)
r1 = conn.getresponse()
print r1.status,r1.reason

Note that the serverPort parameter in the first line is the port the proxy is on and url is the url extracted from the HTTP header received from the browser when it makes a request for a webpage.

So I seem to be getting an error when I run my proxy and have the browser type in an address such as http://www.google.com or http://www.getmetal.org.

The error is:-

socket.gaierror: [Errno 8] nodename nor servname provided, or not known

There is also a trace:-

http://i.stack.imgur.com/UgZwD.png

If anyone has any suggestions as to what the problem may be I'd be delighted. Here is code for the proxy server: NOTE: IF you are testing this,there may be some indentation issues due to having to put everything 4 spaces to the right to have it display as code segment

from socket import *
import httplib 
import webbrowser
import string

serverPort = 2000
serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind(('', serverPort))
serverSocket.listen(2)
urlList=['www.facebook.com','www.youtube.com','www.twitter.com']
print 'The server is ready to receive'

while 1:
    connectionSocket, addr = serverSocket.accept()
    print addr
    req= connectionSocket.recv(1024)


    #parse Get request here abnd extract url
    reqHeaderData= req.split('\n')

    newList=[]

    x=0
    while x<len(reqHeaderData):

        st=reqHeaderData[x]
        element= st.split(' ')
        print element
        newList.append(element)
        x=x+1

   print newList[0][1]
   url = newList[0][1] 
   url= url[:-1]


  for i in urlList:
      if url ==i:

       raise Exception("The website you are trying to access is     blocked")




    connectionSocket.send('Valid')


    print(url)
    conn = httplib.HTTPSConnection(url,serverPort)
    print conn
    conn.request("GET",url)
    print "request printed"
    r1 = conn.getresponse()
    print r1.status,r1.reason
    print r1
    #200 OK

    data = r1.read()

    x= r1.getheaders()

    for i in x:
        print i


    connectionSocket.close()
1
Try going to 8.8.8.8. - Koga
What exactly do you mean? - Hassan Ali
I want to test if the issue is one with dns or if it is otherwise. - Koga
I got the same error yeah - Hassan Ali
Let me try to replicate your situation. - Koga

1 Answers

8
votes

Here's a common mistake I see...

url="https://foo.tld"
port=443

conn=httplib.HTTPConnection(url,port)

This won't work because of the "https://"...

You should do this instead:

url="foo.tld"
port=443

conn=httplib.HTTPConnection(url,port)

This would work. I'm not sure if this is your specific problem, but it is certainly something to verify.