云算法资源有限,请合理使用!
目录
一、 中转执..........................................................................................................................................1
GET................................................................................................................................................... 1
POST................................................................................................................................................ 2
SOCKET...........................................................................................................................................2
二、 云端部署代码执行......................................................................................................................... 2
Python...............................................................................................................................................3
Nodejs...............................................................................................................................................3
Php.................................................................................................................................................... 4
Go1....................................................................................................................................................4
Java8.................................................................................................................................................6
一、中转执行
顾名思义,是通过验证服务器中转 api 请求,来实现简介获取数据.
适用场景:
1. 防止破解者直接抓包盗取应用程序关 API
2. API,且不想将 API 暴露给前
GET
API
例子
:
https://api.thecatapi.com/v1/images/search?size=full
https://api.thedogapi.com/v1/images/search?size=full
http://api.quotable.io/random
https://api.xygeng.cn/one
https://opentdb.com/api.php?amount=3&category=11&difficulty=easy&type=multiple
POST
API
例子
:
URL
Body
Response
https://reqres.in/
api/users
{"name":"morpheus","job
":"leader"}
{"{\r\n \"name\": \"morpheus\",\r\n \"job\":
\"leader\"\r\n}\r\n":"","id":"163","createdAt":"202
4-12-18T14:31:16.487Z"}
https://postman-e
cho.com/post
{"name":"John
Doe","age":30,"city":"Ne
w York"}
{"args":{},"data":"","files":{},"form":{"{\"name\":\"J
ohn Doe\",\"age\":30,\"city\":\"New
York\"}":""},"headers":{"host":"postman-echo.com
","x-request-start":"t1734532438.000","connection
":"close","content-length":"46","x-forwarded-proto
":"https","x-forwarded-port":"443","x-amzn-trace-i
d":"Root=1-6762dd55-5629cb714dc25ce2198af3f0
","user-agent":"Mozilla/5.0 (Windows NT 6.1;
WOW64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/50.0.2661.87
Safari/537.36","accept":"*/*","referer":"https://po
stman-echo.com/post","accept-language":"zh-cn",
"content-type":"application/x-www-form-urlencod
ed","cache-control":"no-cache"},"json":{"{\"name\
":\"John Doe\",\"age\":30,\"city\":\"New
York\"}":""},"url":"https://postman-echo.com/post
"}
SOCKET
尚未完善
-
暂时停用
.2024
12
19
14:23:32
二、云端部署代码执行
与中
,
此行
,
云端
.
代码
可放
,
服务将代动部到沙境当
,
防止者盗
.
资源
,
不要
开采内存和频繁网络交互
!
Python
支持:
Python2.7
Python3.6
DefaultPython2.7
例子:下载例子源码
index_py_code.py
def process_string_and_int(event, context):
# 从事件中获取参数
input_string = event.get("input_string")#拿到一个参数,名字是
input_string
input_int = event.get("input_int")#拿到另一个参,名字是 input_int ,
此类推...
# 对输入的字符串和整数进行处理
result = f"你传入的字符串是: {input_string}, 你传入的整数是: {input_int}"
return result
参数
:
必须是
JSON
{"input_string":"Hello","input_int":123}
执行路径
:
文件名
.
函数名
index_py_code.process_string_and_int
上传是.zip 文件,需要将代码压缩到 zip
Nodejs
Nodejs6.10
Nodejs8.9
Nodejs10.15
Nodejs12.16
代码例子
:
下载例子源码
node_js_code.js
exports.processStringAndInt = async (event, context) => {
// 从事件中获取参数
const inputString = event.input_string; // 获取 input_string 参数
const inputInt = event.input_int; // 获取 input_int 参数
// 对输入的字符串和整数进行处理
const result = `你传入的字符串是: ${inputString}, 你传入的整数:
${inputInt}`;
// 返回处理结果
return result;
};
参数:必须是 JSON
{"input_string":"Hello","input_int":123}
执行路径
:
文件名
.
函数名
node_js_code.processStringAndInt
上传是.zip 文件,需要将代码压缩到 zip
下载例子源码
Php
Php5.2
Php7.4
代码例子
:
下载例子源码
php_code.php
<?php
function processStringAndInt($event, $context) {
// 从事件中获取参数
$inputString = isset($event['input_string']) ?
$event['input_string'] : ''; // 获取 input_string 参数
$inputInt = isset($event['input_int']) ? $event['input_int'] :
0; // 获取 input_int 参数
// 对输入的字符串和整数进行处理
$result = "你传入的字符串是: $inputString, 你传入的整数是: $inputInt";
// 返回处理结果
return $result;
}
?>
参数:必须是 JSON
{"input_string":"Hello","input_int":123}
执行路径
:
文件名
.
函数名
php_code.processStringAndInt
Go1
需要编译
,
上传二进制文
代码例子
:
下载例子源码
go_code.go
package main
import (
"context"
"fmt"
"mymodule/cloudfunction" // 使用模块路径进行导入
)
// 处理云函数事件
func hello(ctx context.Context, event interface{}) (string, error) {
// 断言 event map 类型
eventData, ok := event.(map[string]interface{})
if !ok {
return "", fmt.Errorf("event is not a valid map")
}
// 获取参数
inputString, ok := eventData["input_string"].(string)
if !ok {
return "", fmt.Errorf("input_string is missing or not a valid
string")
}
inputInt, ok := eventData["input_int"].(float64) // JSON 中的数字类型
默认为 float64
if !ok {
return "", fmt.Errorf("input_int is missing or not a valid integer")
}
// 输出参数
fmt.Printf("Received string: %s\n", inputString)
fmt.Printf("Received integer: %d\n", int(inputInt))
// 返回结果
return fmt.Sprintf("你传入的字符串是: %s, 你传入的整数是: %d",
inputString, int(inputInt)), nil
}
func main() {
// 使用 cloudfunction 包的 Start 函数来启动云函数
cloudfunction.Start(hello)
}
目录结构
:
/go
── go_code.go
└── cloudfunction
└── entry.go
└── function.go
└── handler.go
└── panic.go
└── messages
└── messages.go
└── functioncontext
└── context.go
Linux:
go mod init mymodule
GOOS=linux GOARCH=amd64 go build -o main go_code.go
zip main.zip main
Windows:
set GOOS=linux
set GOARCH=amd64
go mod init mymodule
go build -o main go_code.go
参数:必须是 JSON
{"input_string":"Hello","input_int":123}
执行路径
:
文件名
main
上传是
.zip
文件
,
需要将代码编译为二进制文件然后对其压缩
.zip
Java8
需要编译
,
上传
jar
代码例子
:
下载例子源码
CloudFunction.java
package zone_example;
import java.util.Map;
public class CloudFunction {
public static String processStringAndInt(Map<String, Object> event) {
// Get arg
String inputString = (event.containsKey("input_string")) ? (String)
event.get("input_string") : ""; // arg1_name=>input_string
Integer inputInt = (event.containsKey("input_int")) ? ((Number)
event.get("input_int")).intValue() : 0; //arg2_name=>input_int
// return body
return String.format("arg1: %s, arg2: %d", inputString, inputInt);
}