腾讯云函数代理
网上好多代码都是抄袭的,也有一些BUG,例如在3.6 做proxy 请求shell会404
解决如下:
- 原因获取headers中有host,直接带入requests headers,导致host不一致需要删除
- 支持GET请求转发
- 解决content-length
# -*- coding: utf8 -*- import requests import json def geturl(urlstr): jurlstr = json.dumps(urlstr) dict_url = json.loads(jurlstr) return dict_url['u'] def send_post(event): """ POST 代理 """ url = geturl(event['queryString']) data = event['body'] headers = event['headers'] del headers['host'] del headers['content-length'] headers['user-agent'] = "Mozilla/5.0 (Linux; U; Android 4.1.2; zh-cn; GT-I9300 Build/JZO54K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 MicroMessenger/5.2.380" resp = requests.post(url=url, data=data, headers=headers, verify=False) response={ "isBase64Encoded": False, "statusCode": 200, "headers": {'Content-Type': 'text/html;charset='+resp.apparent_encoding}, "body": resp.text, } return response def send_get(event): """ GET 请求 """ url = geturl(event['queryString']) headers = event['headers'] del headers['host'] # del headers['content-length'] headers['user-agent'] = "Mozilla/5.0 (Linux; U; Android 4.1.2; zh-cn; GT-I9300 Build/JZO54K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 MicroMessenger/5.2.380" resp = requests.get(url=url, headers=headers, verify=False) response={ "isBase64Encoded": False, "statusCode": 200, "headers": {'Content-Type': 'text/html;charset='+resp.apparent_encoding}, "body": resp.text, } return response def main_handler(event, context): try: if event['httpMethod'] == "POST": return send_post(event) if event['httpMethod'] == "GET": return send_get(event) except Exception as e: response = { "isBase64Encoded": False, "statusCode": 200, "headers": {'Content-Type': 'text/html;charset='}, "body": str(e) } return response
【版权所有@Sevck 博客地址http://www.cnblogs.com/sevck】 可以转载,注明出处.