Python 如何将下面的示例代码转换为接受多个输入?

Python 如何将下面的示例代码转换为接受多个输入?,python,web-services,flask,Python,Web Services,Flask,我想从我编写的一个简单的发票应用程序创建一个web服务。我希望它能从ApacheFop返回json和pdf文件。我不想要html网页,我将从python桌面应用程序访问该服务 我可以忽略文档的模板部分吗 我遇到的最大问题是接受函数的多个参数 @app.route('/post/<int:post_id>/<int:user_id>/') def show_post(post_id, user_id): # show the post with the given

我想从我编写的一个简单的发票应用程序创建一个web服务。我希望它能从ApacheFop返回json和pdf文件。我不想要html网页,我将从python桌面应用程序访问该服务

我可以忽略文档的模板部分吗

我遇到的最大问题是接受函数的多个参数

@app.route('/post/<int:post_id>/<int:user_id>/')
def show_post(post_id, user_id):
    # show the post with the given id, the id is an integer
    return 'Post %d' % post_id
from flask import Flask
app = Flask(__name__)

@app.route('/post/<int:post_id>', defaults={'action': 1})
def show_post(post_id, action):
    # show the post with the given id, the id is an integer
    # show the defauls argument: action.
    response = 'Post %d\nyour argument: %s' % (post_id, action)
    return response

if __name__ == '__main__':
    app.run()
如何将下面的示例代码转换为接受多个输入

@app.route('/post/<int:post_id>')
def show_post(post_id):
    # show the post with the given id, the id is an integer
    return 'Post %d' % post_id
@app.route(“/post/”)
def显示_post(post_id):
#显示具有给定id的帖子,id为整数
返回“Post%d”%Post\u id

我是编程新手,对web服务更是如此,如果我以错误的方式进行编程,请告诉我。

您当然可以忽略html部分。Flask是创建web应用程序的一种很好的轻量级方法。如果需要,您可以将json(或其他数据)作为响应返回到所有URL,而完全不考虑html模板

您可以在路由定义中包含所需的任意多个参数/正则表达式,每个参数将为函数创建一个新参数

@app.route('/post/<int:post_id>/<int:user_id>/')
def show_post(post_id, user_id):
    # show the post with the given id, the id is an integer
    return 'Post %d' % post_id
from flask import Flask
app = Flask(__name__)

@app.route('/post/<int:post_id>', defaults={'action': 1})
def show_post(post_id, action):
    # show the post with the given id, the id is an integer
    # show the defauls argument: action.
    response = 'Post %d\nyour argument: %s' % (post_id, action)
    return response

if __name__ == '__main__':
    app.run()
@app.route('/post//'))
def show_post(post_id,user_id):
#显示具有给定id的帖子,id为整数
返回“Post%d”%Post\u id

烧瓶是这项工作的合适工具吗

Flask是一个微型python web框架,如瓶子或webpy。在我看来,极简主义web框架非常适合您的工作

不要混淆URL的可变部分“变量规则”和函数的“经典”参数

@app.route('/post/<int:post_id>/<int:user_id>/')
def show_post(post_id, user_id):
    # show the post with the given id, the id is an integer
    return 'Post %d' % post_id
from flask import Flask
app = Flask(__name__)

@app.route('/post/<int:post_id>', defaults={'action': 1})
def show_post(post_id, action):
    # show the post with the given id, the id is an integer
    # show the defauls argument: action.
    response = 'Post %d\nyour argument: %s' % (post_id, action)
    return response

if __name__ == '__main__':
    app.run()
从烧瓶导入烧瓶
app=烧瓶(名称)
@app.route(“/post/”,默认值={'action':1})
def show_post(post_id,action):
#显示具有给定id的帖子,id为整数
#显示defauls参数:action。
响应='Post%d\n您的参数:%s'(Post\u id,操作)
返回响应
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app.run()

谢谢大家的回答,他们帮了大忙。下面是一些工作示例代码,从无参数逐步升级到参数,然后是带有json响应的参数

希望下面的代码能帮助一些人

from flask import Flask
from flask import jsonify
app = Flask(__name__)

@app.route('/')
def helloworld():
    response = 'HelloWorld'
    return response

@app.route('/mathapi/<int:x>/<int:y>/',  methods = ['GET'])
def math(x, y):
    result = x + y # Sum x,t -> result
    response = '%d + %d = %d' %(x, y, result) #Buld response
    return response #Return response

@app.route('/mathapijson/<int:x>/<int:y>/', methods = ['GET'])
def mathjs(x, y):
    result = x + y #Sum x,t -> result
    data = {'x'  : x, 'y' : y, 'result' : result} #Buld arrary
    response = jsonify(data) #Convert to json
    response.status_code = 200 #Set status code to 200=ok
    response.headers['Link'] = 'http://localhost'

    return response #return json response

if __name__ == '__main__':
app.run(debug=True)
从烧瓶导入烧瓶
从flask导入jsonify
app=烧瓶(名称)
@应用程序路径(“/”)
def helloworld():
响应='HelloWorld'
返回响应
@app.route('/mathapi//',methods=['GET'])
定义数学(x,y):
结果=x+y#和x,t->result
响应=“%d+%d=%d%”(x,y,结果)#Buld响应
返回响应#返回响应
@app.route('/mathapijson//',methods=['GET'])
def mathjs(x,y):
结果=x+y#和x,t->result
数据={'x':x,'y':y,'result':result}#Buld数组
response=jsonify(数据)#转换为json
response.status_code=200#将状态代码设置为200=ok
response.headers['Link']='http://localhost'
返回响应#返回json响应
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app.run(debug=True)
用法:

localhost:port/-Output-HelloWorld

localhost:port/mathapi/3/4/-output=3+4=7


localhost:port/mathapi/mathapijson/3/4/-output={“y”:4,“x”:3,“result”:7}

不知道为什么有人刚刚给了你-1。。。问题似乎很好。。。也许不要问它是否是正确的工具,而是问如何使用这个工具。。。这是一个工具,它当然可以完成你想要的。在任何情况下,如果你忽略模板部分,没有人会来敲你的头。如果烧瓶的其余部分适合您,请务必使用烧瓶!:-)我给了-1分。AFAIC,“是否是这项工作的正确工具”是一个主观问题,会引起争论。另一方面,“如何在flask中接受多个输入”是StackOverflow的一个好问题。我希望这是一个真实的问题。将问题标题编辑为MarkHildreth建议,感谢我第一篇帖子中的-1。我同意这份工作的正确工具是主观的(删除)。@MartynJones:我确实相信你关于“可以用来托管web服务”的问题是个好问题,但措辞不同。您已经给出了一个您认为可能不存在的原因(多个输入),现在有了如何正确执行的答案。如果您认为flask与HTML呈现的关系过于紧密,那么另一个问题“我如何使用flask在响应中返回JSON/XML/而不是HTML”可能会很有帮助。谢谢,这非常有效,感谢regex链接,它将在handyThanks中出现,遗憾的是我还不能投票支持。有些人请投洛杰尼和dm03514给我:)