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

2024-04-29 00:18:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写我自己的非常基本的代理服务器,我只想让我的代码发送请求并接收响应,然后将其发送回浏览器。 我的代码(我从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),是不是因为服务器检测到它是一个自制的代理服务器并关闭了连接?如果是的话,你能给我介绍更高级的东西吗。在


Tags: urlclosedataserverportsocketconnreply