84669 人が学習中
152542 人が学習中
20005 人が学習中
5487 人が学習中
7821 人が学習中
359900 人が学習中
3350 人が学習中
180660 人が学習中
48569 人が学習中
18603 人が学習中
40936 人が学習中
1549 人が学習中
1183 人が学習中
32909 人が学習中
假设我写了一个小脚本,里边是一个函数,接受两个参数,返回结果是两个参数之和
现在我需要怎么做,才能远程向这个小脚本提交参数并执行呢?
我大概知道好像要搭建一个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
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