Python:Multi-thread not works as epected -


i want start project learn python, , chose write simple web proxy.

in case, thread seems null request, , python rasie exception:

first_line:  http://racket-lang.org/ http/1.1 connect to: racket-lang.org 80 first_line: exception in thread thread-2: traceback (most recent call last):   file "c:\python27\lib\threading.py", line 551, in __bootstrap_inner     self.run()   file "c:\python27\lib\threading.py", line 504, in run     self.__target(*self.__args, **self.__kwargs)   file "fakespider.py", line 37, in proxy     url = first_line.split(' ')[1] indexerror: list index out of range  first_line: first_line:   http://racket-lang.org/plt.css http/1.1get http://racket-lang.org/more.css http/1.1  connect to:connect to:  racket-lang.orgracket-lang.org  8080 

my code simple. don't know what's going on, appreciated:)

from threading import thread time import time, sleep import socket import sys  recv_buffer = 8192 debug = true  def recv_timeout(socks, timeout = 2):     socks.setblocking(0);     total_data = []     data = ''     begin = time()     while true:         if total_data , time() - begin > timeout:             break         elif time() - begin > timeout * 2:             break         try:             data = socks.recv(recv_buffer)             if data:                 total_data.append(data)                 begin = time()             else:                 sleep(0.1)         except:             pass     return ''.join(total_data)  def proxy(conn, client_addr):     request = recv_timeout(conn)      first_line = request.split('\r\n')[0]     if (debug):         print "first_line: ", first_line     url = first_line.split(' ')[1]      http_pos = url.find("://")     if (http_pos ==  -1):         temp = url     else:         temp = url[(http_pos + 3):]      port_pos = temp.find(":")     host_pos = temp.find("/")     if host_pos == -1:         host_pos = len(temp)      host = ""     if (port_pos == -1 or host_pos < port_pos):         port = 80         host = temp[:host_pos]     else:         port = int((temp[(port_pos + 1):])[:host_pos - port_pos - 1])         host = temp[:port_pos]      print "connect to:", host, port      try:         s = socket.socket(socket.af_inet, socket.sock_stream)         s.connect((host, port))         s.send(request)          data = recv_timeout(s)         if len(data) > 0:             conn.send(data)         s.close()         conn.close()     except socket.error, (value, message):         if s:             s.close()         if conn:             conn.close()         print "runtime error:", message         sys.exit(1)    def main():     if len(sys.argv) < 2:         print "usage: python fakespider.py <port>"         return sys.stdout      host = "" #blank localhost     port = int(sys.argv[1])      try:         s = socket.socket(socket.af_inet, socket.sock_stream)         s.bind((host, port))         s.listen(50)      except socket.error, (value, message):         if s:             s.close()         print "could not open socket:", message         sys.exit(1)      while 1:         conn, client_addr = s.accept()         t = thread(target=proxy, args=(conn, client_addr))         t.start()      s.close()  if __name__ == "__main__":     main() 

the stack trace see says everything:

url = first_line.split(' ')[1]   indexerror: list index out of range 

apparently result of splitting variable first_line not list having more 1 element, assumed. contains different expected. see contains print out:

print first_line 

or use debugger.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -