简单代理服务器“按对等方重置连接”错误

时间:2018-01-27 23:35:55

标签: python sockets proxy proxy-server

我正在尝试编写自己的基本代理服务器,我只想让我的代码发送请求并收到响应并将其发送回浏览器。 我的代码(我从this教程获得)是:

import socket, sys 
from thread import * 

try:
    listeningPort = int(raw_input("[*] : inter port to listen to "))
    print "your port is " + str(listeningPort)

except KeyboardInterrupt :
    print "\n[*] : User Requested Interrupt ! "
    print "[*] : Bye :D ! "
    sys.exit();


maxConnections = 5
bufferSize = 4096


def start():
    try:
        s = socket.socket(socket.AF_INET , socket.SOCK_STREAM) # init socket
        s.bind(('' , listeningPort)) # binf with listening port 
        s.listen(maxConnections)
        print("[*] : Initializing sockets .. Done")
        print("[*] : listening on port [%d] \n" , listeningPort)
except Exception , e:
    print("[*] : Unable to initialize socket" + str(e))
    sys.exit(2)

while 1:
    try :
        print "accepting connection"
        conn , addr = s.accept() # accpet connection from browser
        data = conn.recv(bufferSize) # get client data
        start_new_thread(conn_string , (conn , data ,addr)) # start a new thread 

    except KeyboardInterrupt:
        s.close()
        print "\nWer'e shutting Down ! "
        print "Bye ! "
        sys.exit(1)

s.close()

def conn_string(conn , data , addr):
    # accepts data from the browser 
    print "herererer"
    try:
        firstLine = data.split('\n')[0]
        url = firstLine.split(' ')[1]
        print "first line " + str(firstLine)
        print "url iss : " + str(url)
        httpPos = url.find("://")

    if httpPos == -1:
        temp = url
    else:
        temp = url[(httpPos + 3):]


    portPoss = temp.find(":") # position of the port 
    webServerPos = temp.find("/") # end of the web server 

    if webServerPos == -1:
        webServerPos = len(temp)

    webServer = ""
    port = -1

    if (portPoss == -1 or webServerPos < portPoss):
        port = 80
        webServer = temp[:webServerPos]
    else: 
        #sepecific port 
        port = int((temp[(portPoss + 1):])[:webServerPos - portPoss - 1])
        webServer = temp[:portPoss]

    proxy_server(webServer , port , conn , addr , data)

except Exception , e :
    print "expectionnn " + str(e)



def proxy_server(webServer , port , conn , addr , data):
    try:
        s = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
        # print "here1"
        s.connect((webServer , port))
        # print "here2"
        s.send(data)
        # print "here3"

    while 1:
        #Read reply or data from end web server
        #print "here4"
        reply = s.recv(bufferSize)

        print "reply count isss" + str(len(reply))
        if (len(reply) > 0):
            conn.send(reply)
            #Send notificaiton to prexy server [US]
            dar = float(len(reply))
            dar = float(dat / 1024)
            dar = "%.3s" % (str(dar))
            dar = "%s KB" % (dar)
            print "[*] Request Done : %s => %s <= " % (str(addr[0]) , str(dar))

        else:
            #break connection if receiving data failed
            break

    s.close()
    conn.close()

except socket.error , (value , message):
    print "proxy_server expection " 
    print "value " + str(value)
    print "message : " + str(message)
    s.close()
    conn.close()
    sys.exit(1)




start()

这是一个非常基本的代理服务器。 我一直得到错误并且连接被对等方重置(错误号54),是因为服务器检测到它是自制的代理服务器并关闭了连接?如果是的话,你能不能把我推荐给更高级的东西。

0 个答案:

没有答案