Python Web代理无法加载HTTPS站点

时间:2017-03-20 16:05:27

标签: python firefox proxy

我正在用Python编写Web代理。我有一个问题,任何时候我尝试加载HTTPS网站(在Firefox中)我收到安全警告错误(我将在下面发布截图)。它会出现(从终端上的程序输出)我的代理发送和接收来自Web服务器的数据,但Firefox没有加载它。我可以像example.com一样加载HTTP网站。我知道我应该为此使用线程,但我想逐步构建。这是我的代码和一些屏幕抓取,以更好地说明正在发生的事情:非常感谢您的帮助!

import sys
import socket

# Constants
PORT = 9876
MAX_BUFFER = 4096
HOST = ''
BACKLOG = 5

def main():
    ''' This is the main function'''
# Start socket
    try:
        clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        clientSocket.bind((HOST, PORT))
        clientSocket.listen(BACKLOG)
        print 'Proxy running on %s:%d' % (HOST, PORT)

    except socket.error, (message):
        print message

        if clientSocket:
            clientSocket.close()

        sys.exit(1)

# Listen for requests
    while 1:
    # conn is the socket we can send and receive to/from the client
        print 'Listening to client'
        conn, client_addr = clientSocket.accept()
        request = conn.recv(MAX_BUFFER)

    # Parsing
        webserver, port = ParseHTTP(request)
    # Heard from client, now forward to server
        try:
            print 'Sending request to server'
            serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            serverSocket.connect((webserver, port))
            serverSocket.send(request)

            while 1:
                data = serverSocket.recv(MAX_BUFFER)

            # while there is data to receive from server
                if len(data) > 0:
                    conn.send(data)

                else:
                    print 'Completed sending data to client'
                    break

            serverSocket.close()
            conn.close()

        except socket.error, (message):
            print message

            if clientSocket:
                clientSocket.close()

            if conn:
                conn.close()

            if serverSocket:
                serverSocket.close()

            sys.exit(1)

    print 'Goodbye'
#******************End of main*********************

def ParseHTTP(request):
    first_line = request.split('\n')[0]
    print 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(':')

    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:
        port = int((temp[(port_pos + 1):])[:webserver_pos - port_pos -1])
        webserver = temp[:port_pos]

    return webserver, port
#******************End of ParseWebServer()*********************

if __name__ == '__main__':
    main()

The security error message from Firefox

My proxy settings in Firefox, something wrong here?

You can see my proxy is fetching data from the webserver but Firefox throws that error screen

0 个答案:

没有答案