Python实现请求代理

时间:2024-03-11 22:04:04

前端开发调试的时候,通常是localhost,而接口不在本机,会有跨域问题。通过http.server 实现的请求代理,解决了跨域问题并且实现登录。

class MyRequestHandler(SimpleHTTPRequestHandler):

    def addHeader(self):
        self.send_header("Access-Control-Allow-Origin", "*")
        self.send_header("Access-Control-Allow-Headers", "*")
        self.send_header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE")
        self.send_header("Content-type", "text/html")

    def do_GET(self):
        print(\'Get请求:\'+self.path)
        rep_get = requests.get(serverurl + self.path, cookies=cookie_jar)
        if rep_get.status_code == 200:
            self.send_response(200)
            self.addHeader()
            self.end_headers()
            self.wfile.write(rep_get.text.encode("utf-8"))
        else:
            self.Login()
            rep_get = requests.get(serverurl + self.path, cookies=cookie_jar)
            self.send_response(200)
            self.addHeader()
            self.end_headers()
            self.wfile.write(rep_get.text.encode("utf-8"))

    def do_POST(self):
        print(\'Post请求:\'+self.path)
        request_contentType = str(self.headers["Content-Type"])
        if request_contentType.find(\'application/x-www-form-urlencoded\')>-1:
            data = str(self.rfile.read(int(self.headers["content-length"])),\'UTF-8\')

            #data = str(self.rfile.readlines(), \'UTF-8\')  # 先解码
            p1 = urllib.parse.parse_qs(urllib.parse.unquote(data))  # 解释参数
            rel_par={}
            for it in p1:
                rel_par[it]=p1[it][0]

            rep_post = requests.post(serverurl + self.path, cookies=cookie_jar,data=rel_par)
            if rep_post.status_code==200:

                self.send_response(200)
                self.addHeader()
                self.end_headers()
                self.wfile.write(rep_post.text.encode("utf-8"))
            else:
                self.Login()
                rep_post = requests.post(serverurl + self.path, cookies=cookie_jar, data=rel_par)
                self.send_response(200)
                self.addHeader()
                self.end_headers()
                self.wfile.write(rep_post.text.encode("utf-8"))

        else:
            req = {"success": "false", "message": "暂时只支持Content-Type=application/x-www-form-urlencoded"}
            self.send_response(200)
            self.addHeader()
            self.end_headers()
            rspstr = json.dumps(req)
            self.wfile.write(rspstr.encode("utf-8"))

    def Login(self):
        isLogin =True
        req_login1 = requests.post(serverurl + \'/SUP/Login/WebLogin\',data={
            \'ocode\': \'\',
            \'UserID\': user,
            \'UserPwd\': pwd,
            \'DataBase\': ucode,
            \'Language\': "zh-CN",
            \'verifyCode\': ""
        })
        if req_login1.status_code == 200:
            if req_login1.text.find(\'success: true\') > 0:
                globals()[\'cookie_jar\']=req_login1.cookies
            else:
                i1 = req_login1.text.find(\'msg\')
                km = json.loads(req_login1.text[i1 + 5:-2])
                if km != None:
                    print(\'开始踢人\')
                    req_kill = requests.post(serverurl + \'/SUP/Login/KillOnlineUser\', data={
                        \'IpAddress\': km[\'IpAddress\'],
                        \'Devicetype\': km[\'Devicetype\'],
                        \'UserId\': km[\'UserId\'],
                        \'SessionID\': km[\'SessionID\']
                    })
                    if req_kill.status_code == 200:
                        print(\'踢人成功,重新登录\')
                        req_login2 = requests.post(serverurl + \'/SUP/Login/WebLogin\', data={
                            \'ocode\': \'\',
                            \'UserID\': user,
                            \'UserPwd\': pwd,
                            \'DataBase\': ucode,
                            \'Language\': "zh-CN",
                            \'verifyCode\': "",
                            \'IsOnlineCheck\': \'1\'
                        })
                        if req_login2.status_code == 200:
                            globals()[\'cookie_jar\'] = req_login2.cookies
                        else:
                            isLogin = False
                            print(\'踢人以后登录返回错误,请检查服务器是否可访问\')
                    else:
                        isLogin = False
                        print(\'踢人接口错误,请检查服务器是否可访问\')
                else:
                    isLogin = False
                    print(\'登录返回错误,请检查服务器是否可访问\')
        else:
            isLogin = False
            print(\'服务器登录返回错误,请检查服务器是否可访问\')
        return isLogin

使用:

try:
    config = configparser.ConfigParser()
    isread = config.read(\'ngproxy.ini\')
    if len(isread) == 0:
        serverurl = input("请输入服务器url: ")
        ucode = input("请输入服务器账套: ")
        user = input("请输入服务器用户: ")
        pwd = input("请输入服务器密码: ")
        port = input("请输入代理端口: ")

        config.add_section(\'userinfo\')
        config.set(\'userinfo\', \'serverurl\', serverurl)
        config.set(\'userinfo\', \'username\', user)
        config.set(\'userinfo\', \'password\', pwd)
        config.set(\'userinfo\', \'ucode\', ucode)
        config.set(\'userinfo\', \'port\', port)
        f = open(\'ngproxy.ini\', "w")
        config.write(f)  # 写进文件
        f.close()
    else:
        serverurl = config[\'userinfo\'][\'serverurl\']
        user = config[\'userinfo\'][\'username\']
        pwd = config[\'userinfo\'][\'password\']
        ucode = config[\'userinfo\'][\'ucode\']
        port = config[\'userinfo\'][\'port\']
    bytes_user=user.encode()
    user = str(base64.b64encode(bytes_user), \'utf-8\')

    bytes_pwd = pwd.encode()
    pwd = base64.b64encode(bytes_pwd)
    pwd = str(pwd, \'utf-8\')

    cookie_jar = RequestsCookieJar()

    server = HTTPServer(("", int(port)), MyRequestHandler)
    print("代理服务器启动成功, serving at http://localhost:" + port)
    server.serve_forever()