扫码关注官方订阅号
假设我写了一个小脚本,里边是一个函数,接受两个参数,返回结果是两个参数之和
现在我需要怎么做,才能远程向这个小脚本提交参数并执行呢?
我大概知道好像要搭建一个Web服务器,但是完全没有头绪,请给出一些方向或是关键词,我再去google,感谢
走同样的路,发现不同的人生
Python3 & flask_restful
pip install flask_restful
代码
from flask import Flask, jsonify from flask_restful import Resource, Api, reqparse app = Flask(__name__) api = Api(app) class HelloWorld(Resource): def get(self): parser = reqparse.RequestParser() parser.add_argument('a', type=int, required=True) parser.add_argument('b', type=int, required=True) args = parser.parse_args() result = { "a": args["a"], "b": args["b"], "sum": args["a"] + args["b"] } return jsonify(result) api.add_resource(HelloWorld, '/') if __name__ == '__main__': app.run()
效果
浏览器访问 http://127.0.0.1:5000/?a=1000...
https://mirrors.segmentfault....
照着1.2.1小节里面的Hello Tornado改改, 大概这意思:
class IndexHandler(tornado.web.RequestHandler): def get(self): p1 = self.get_argument('p1') p2 = self.get_argument('p2') s = func(p1, p2) self.write('{}+{}={}'.format(p1,p2,s))
如果你使用HTTP协议(通常是80端口)的话,就是你提出的搭建一个Web服务器,那么推荐:
Flask: http://flask.pocoo.org/
Django: https://www.djangoproject.com/
Tornando:http://www.tornadoweb.org/en/...
等Python Web框架
如果你使用SSH协议的话(22端口),那么推荐使用Fabric做远程调用:http://www.fabfile.org/
基于 aiohttp 的:
from aiohttp import web async def add(request): foo = request.match_info.get('foo') bar = request.match_info.get('bar') res = "{}+{}={}".format(foo, bar, int(foo) + int(bar)) return web.Response(text=res) app = web.Application() app.router.add_get('/{foo}/{bar}', add) web.run_app(app)
运行之后,访问 http://localhost:8080/1/2 可以得到
1+2=3
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
扫描下载App
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
Python3 & flask_restful
pip install flask_restful
代码
效果
https://mirrors.segmentfault....
照着1.2.1小节里面的Hello Tornado改改, 大概这意思:
如果你使用HTTP协议(通常是80端口)的话,就是你提出的搭建一个Web服务器,那么推荐:
Flask: http://flask.pocoo.org/
Django: https://www.djangoproject.com/
Tornando:http://www.tornadoweb.org/en/...
等Python Web框架
如果你使用SSH协议的话(22端口),那么推荐使用Fabric做远程调用:
http://www.fabfile.org/
基于 aiohttp 的:
运行之后,访问 http://localhost:8080/1/2 可以得到
1+2=3