与任何站点的代理连接失败

时间:2017-01-30 13:04:59

标签: python-2.7

大家好我需要一些帮助,当我在浏览器上运行代理服务器时,它给我连接安全连接安全失败

#/!usr/bin/python
import socket
import os
import sys
import thread

try:
   listening_port = int(raw_input("enter the port of proxy: "))
except KeyboardInterrupt:
   print"\n[*] User Requested An Interrupt"
   print"[*] Alpication Exiting ..."
   sys.exit()

max_conn = 50
buffer_size = 4096

def start():
   try:
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.bind(("192.168.1.1",listening_port))
      s.listen(max_conn)
   except Exception, e:
      print "[*] Unabel To initialize Socket",e
      s.close()
      sys.exit(2)

   while 1:
      try:
         conn, addr = s.accept()
         data = conn.recv(buffer_size)
         thread.start_new_thread(conn_string,(conn, data, addr))

      except:
         sys.exit(1)
         s.close()

def conn_string(conn, data, addr):
    #client browser request apears here
    try:
       first_line = data.split('\n')[0]
       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(":")
       webserver_pos = temp.find("/")
       if webserver_pos == -1:
           webserver_pos = len(temp)
           webserver = ""
           port = -1
       if port_pos == -1 or webserver_pos < port_pos:
           port = 80
           webserver = temp[:webserver_pos]
       else:
          #specific port
          port = int((temp[(port_pos+1):])[:webserver_pos-port_pos-1])
          webserver = temp[:port_pos]
       proxy_server(webserver, port, conn, data, addr)
     except Exception, e:
        print e
        pass

def proxy_server(webserver, port, conn, data, addr):
    try:
       s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
       s.connect((webserver, 80))
       s.sendall(data)
       while 1:
          #read reply or data to from end webserver
          reply = s.recv(buffer_size)
          if (len(reply) > 0):
              conn.send(reply)
              #send notification to proxy server
              dar = float(len(reply))
              dar = float (dar / 1024)
              dar = "%.3s" % (str(dar))
              dar = "%s KB" % (dar)
              print "[*] Request Message For Request Complete"
              print "[*] Request Done: %s => %s <=" % (str(addr[0]),addr[1])
              print '\n',data,'\n'
          else:
              break
       s.close()
       conn.close()
    except socket.error, (value, message):
       print socket.error,value,message
       s.close()
       conn.close()
       sys.exit(1)

start()

0 个答案:

没有答案