Python - 从url参数中进行数学运算

时间:2016-04-13 05:40:54

标签: python json parsing url

我的目标只是:

网址类似于:/ add?a = 1& b = 2

我的函数应该获取这些参数的值并简单地添加。但是,我实际上仍然坚持如何实现这一目标。以下是我添加功能的全部内容:

def add():
    a = 0
    b = 1
    c = a + b
    return json.dumps({'c':c})

所以如果/ add?a = 1& b = 2,它应该返回:

{"c": 3}

{"c": 1}

2 个答案:

答案 0 :(得分:1)

你没有清楚地描述这个问题:

是否只需要一个解析函数从url字符串中提取参数并进行数学运算;

或者想要对此网址发出请求并将结果作为回复。

正如你在评论中提到的web2py,它应该是第二个。我没有web2py经验,但我认为它可能与烧瓶相似。

以Flask为例。您需要为map<pair<char, char>, double>添加处理程序。我会忽略导入。

/add

答案 1 :(得分:0)

Python的内置2016-04-13T15:43:20+1000 [Controller 2973] Traceback (most recent call last): 2016-04-13T15:43:20+1000 [Controller 2973] File "/home/romaan/workspacepy/tester/bin/crossbar", line 9, in <module> 2016-04-13T15:43:20+1000 [Controller 2973] load_entry_point('crossbar==0.13.0', 'console_scripts', 'crossbar')() 2016-04-13T15:43:20+1000 [Controller 2973] File "/home/romaan/workspacepy/tester/local/lib/python2.7/site-packages/crossbar/controller/cli.py", line 992, in run 2016-04-13T15:43:20+1000 [Controller 2973] options.func(options, reactor=reactor) 2016-04-13T15:43:20+1000 [Controller 2973] File "/home/romaan/workspacepy/tester/local/lib/python2.7/site-packages/crossbar/controller/cli.py", line 560, in run_command_start 2016-04-13T15:43:20+1000 [Controller 2973] from crossbar.controller.node import maybe_generate_key 2016-04-13T15:43:20+1000 [Controller 2973] File "/home/romaan/workspacepy/tester/local/lib/python2.7/site-packages/crossbar/controller/node.py", line 52, in <module> 2016-04-13T15:43:20+1000 [Controller 2973] from crossbar.worker.router import RouterRealm 2016-04-13T15:43:20+1000 [Controller 2973] File "/home/romaan/workspacepy/tester/local/lib/python2.7/site-packages/crossbar/worker/router.py", line 63, in <module> 2016-04-13T15:43:20+1000 [Controller 2973] from crossbar.worker.testee import WebSocketTesteeServerFactory, \ 2016-04-13T15:43:20+1000 [Controller 2973] File "/home/romaan/workspacepy/tester/local/lib/python2.7/site-packages/crossbar/worker/testee.py", line 44, in <module> 2016-04-13T15:43:20+1000 [Controller 2973] from crossbar.worker.worker import NativeWorkerSession 2016-04-13T15:43:20+1000 [Controller 2973] File "/home/romaan/workspacepy/tester/local/lib/python2.7/site-packages/crossbar/worker/worker.py", line 48, in <module> 2016-04-13T15:43:20+1000 [Controller 2973] from crossbar.common.process import NativeProcessSession 2016-04-13T15:43:20+1000 [Controller 2973] File "/home/romaan/workspacepy/tester/local/lib/python2.7/site-packages/crossbar/common/process.py", line 67, in <module> 2016-04-13T15:43:20+1000 [Controller 2973] from crossbar.common import checkconfig 2016-04-13T15:43:20+1000 [Controller 2973] File "/home/romaan/workspacepy/tester/local/lib/python2.7/site-packages/crossbar/common/checkconfig.py", line 44, in <module> 2016-04-13T15:43:20+1000 [Controller 2973] from autobahn.websocket.protocol import parseWsUrl 2016-04-13T15:43:20+1000 [Controller 2973] ImportError: cannot import name parseWsUrl 支持解析URL本身。然后,就像您一样,您可以使用urllib.parse以该格式输出它。

json.dumps