Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用curl在不同操作系统中调用Flask REST服务方法_Python_Rest_Curl_Flask - Fatal编程技术网

Python 使用curl在不同操作系统中调用Flask REST服务方法

Python 使用curl在不同操作系统中调用Flask REST服务方法,python,rest,curl,flask,Python,Rest,Curl,Flask,我为我的RESTAPI编写了以下POST方法,它是使用Flask构建的。该方法接收一个参数,即无线电台url @app.route('/todo/api/v1.0/predvajaj', methods=['POST']) def create_task(): print "Expression value: " + str(not request.json or not 'title' in request.json) if not request.json or not 't

我为我的RESTAPI编写了以下POST方法,它是使用Flask构建的。该方法接收一个参数,即无线电台url

@app.route('/todo/api/v1.0/predvajaj', methods=['POST'])
def create_task():
    print "Expression value: " + str(not request.json or not 'title' in request.json)
    if not request.json or not 'title' in request.json:
        abort(400)

    link=request.json['title']
    print "Link value: " + link
    cmd = "pkill sox"
    os.system(cmd)
    time.sleep(2)   
    #link = "http://www.radiostationurl.m3u"
    cmd = "sox -t mp3 " + link + " -t wav -r 22050 -c 1 - | sudo ../pifm - 90.5 &"
    os.system(cmd)
    return jsonify({'status': "ok"}), 201
该API在一个ip地址为192.168.0.200的Raspberry Pi上运行。我尝试使用curl工具在本地(在Pi上)测试该方法。这很好:

curl -i -H "Content-Type: application/json" -X POST -d '{"title":"http://www.radiostationurl.m3u"}' http://192.168.0.200:5000/todo/api/v1.0/predvajaj
然后,我尝试在同一局域网内用一台计算机(运行Windows)用同一命令和工具对其进行测试,但出现以下错误:

HTTP/1.0 400 BAD REQUEST
Content-Type: text/html
Content-Length: 192
Server: Werkzeug/0.10.4 Python/2.7.3
Date: Wed, 05 Aug 2015 11:06:05 GMT

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
因此,问题在于if表达式的计算。有谁能告诉我为什么它不能评估

编辑:按照@meuh的建议,尝试使用
-v
开关运行curl。内容长度不同

Pi:

*即将连接()到192.168.0.200端口5000(#0)
*正在尝试192.168.0.200。。。
*连接的
*连接到192.168.0.200(192.168.0.200)端口5000(#0)
>POST/todo/api/v1.0/predvajaj HTTP/1.1
>用户代理:curl/7.26.0
>主持人:192.168.0.200:5000
>接受:*/*
>内容类型:application/json
>内容长度:51
>
*上传已完全发送:51个字节中有51个
*附加材料未精细转移。c:1037:0
*附加材料未精细转移。c:1037:0
*附加材料未精细转移。c:1037:0
*HTTP 1.0,假设在正文之后关闭
窗口:

* About to connect() to 192.168.0.200 port 5000 (#0)
*   Trying 192.168.0.200...
* Connected to 192.168.0.200 (192.168.0.200) port 5000 (#0)
> POST /todo/api/v1.0/predvajaj HTTP/1.1
> Host: 192.168.0.200:5000
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 49
>
* upload completely sent off: 49 out of 49 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 400 BAD REQUEST
< Content-Type: text/html
< Content-Length: 192
< Server: Werkzeug/0.10.4 Python/2.7.3
< Date: Wed, 05 Aug 2015 13:50:51 GMT
<
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
* Closing connection 0
*即将连接()到192.168.0.200端口5000(#0)
*正在尝试192.168.0.200。。。
*连接到192.168.0.200(192.168.0.200)端口5000(#0)
>POST/todo/api/v1.0/predvajaj HTTP/1.1
>主持人:192.168.0.200:5000
>用户代理:curl/7.43.0
>接受:*/*
>内容类型:application/json
>内容长度:49
>
*上传已完全发送:49个字节中的49个
*HTTP 1.0,假设在正文之后关闭

*正在关闭连接0

我忽略了关于这个问题的一个注释,在

注意:如果您在Windows上,并且使用bash的Cygwin版本的curl,那么上面的命令就可以正常工作。但是,如果您在常规命令提示符下使用curl的本机版本,则需要在请求主体内发送双引号。基本上,在Windows上,您必须使用双引号来括起请求主体,然后在请求主体内,通过依次写入三个双引号来转义双引号

就我而言,正确的命令是:

curl -i -H "Content-Type: application/json" -X POST -d "{"""title""":"""http://www.radiostationurl.m3u"""}" http://192.168.0.200:5000/todo/api/v1.0/predvajaj

在两台机器上的curl中使用
-v
而不是
-i
,以便比较它们各自发送的内容。
* About to connect() to 192.168.0.200 port 5000 (#0)
*   Trying 192.168.0.200...
* Connected to 192.168.0.200 (192.168.0.200) port 5000 (#0)
> POST /todo/api/v1.0/predvajaj HTTP/1.1
> Host: 192.168.0.200:5000
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 49
>
* upload completely sent off: 49 out of 49 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 400 BAD REQUEST
< Content-Type: text/html
< Content-Length: 192
< Server: Werkzeug/0.10.4 Python/2.7.3
< Date: Wed, 05 Aug 2015 13:50:51 GMT
<
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
* Closing connection 0
curl -i -H "Content-Type: application/json" -X POST -d "{"""title""":"""http://www.radiostationurl.m3u"""}" http://192.168.0.200:5000/todo/api/v1.0/predvajaj