0

I have a project about WebProxyServer. When I run the code, it has error "list index out of range." I can't find where is a mistake.

def proxy_thread(conn, client_addr):

# get the request from browser
request = conn.recv(MAX_DATA_RECV)
#print(" Test " + request)
request = request.decode()
#print(" Test " + request)

# parse the first line 
first_line = request.split('\n')[0]
print("Test first_line " + first_line)
#first_line = request.split('\n')
#first_line = first_line[0]

# get url
url = first_line.split(' ')[1]
print(" Test url " + url)

for i in range(0,len(BLOCKED)):
    if BLOCKED[i] in url:
        printout("Blacklisted",first_line,client_addr)
        conn.close()
        sys.exit(1)


#printout("Request",first_line,client_addr)
# print "URL:",url
# print

# find the webserver and port
http_pos = url.find("://")          # find pos of ://
if (http_pos==-1):
    temp = url
else:
    temp = url[(http_pos+3):]       # get the rest of url

port_pos = temp.find(":")           # find the port pos (if any)

# find end of web server
webserver_pos = temp.find("/")
if webserver_pos == -1:
    webserver_pos = len(temp)

webserver = ""
port = -1
if (port_pos==-1 or webserver_pos < port_pos):      # default port
    port = 80
    webserver = temp[:webserver_pos]
else:       # specific port
    port = int((temp[(port_pos+1):])[:webserver_pos-port_pos-1])
    webserver = temp[:port_pos]

try:
    # create a socket to connect to the web server
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
    s.connect((webserver, port))
    s.sendto(request.encode(),(webserver,port))
    #s.send(request)         # send request to webserver

    while 1:
        # receive data from web server
        data = s.recv(MAX_DATA_RECV)

        if (len(data) > 0):
            # send to browser
            conn.send(data)
        else:
            break
    s.close()
    #conn.close()
except socket.error (value, message):
    if s:
        s.close()
    if conn:
        conn.close()
    printout("Peer Reset",first_line,client_addr)
    sys.exit(1)

I try to connect to www.google.com and here is the output:

No port given, using :8080 (http-alt) Proxy Server Running on : 8080 Test first_line CONNECT www.google.com:443 HTTP/1.1

Test url www.google.com:443 Test first_line

Unhandled exception in thread started by Traceback (most recent call last): File "C:\Users\dangt\Desktop\test\test1.py", line 85, in proxy_thread url = first_line.split(' ')[1] IndexError: list index out of range

  • are you sure `first_line` has a space in it? if it doesnt you will be trying to access an index which doesnt exist – Chris Doyle Mar 18 '20 at 17:07
  • it has. I print out the first_line and it shows "CONNECT www.google.com:443 HTTP/1.1" I also print out url and it shows "www.google.com:443" but then it has the error list index out of range. – Dang Truong Mar 18 '20 at 18:17

1 Answers1

0

I found the answer. I have to handle for the instant of first_line = 0

if first_line = 0, conn.close()