网上好多代码都是抄袭的,也有一些BUG,例如在3.6 做proxy 请求shell会404
解决如下:
# -\*- 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