我在localhost上有一个运行正常的flask服务器,当测试为简单的python字典提供服务时,它可以正常地提供GET和POST请求。现在我正在尝试使用下面的PHP脚本上传和读取一个文本文件的内容(文件和脚本都在同一个Windows目录中):
<?
/*testscript.php*/
$url='http://localhost/transaction'
$ch = curl_init($url);
$cfile = new CURLFile('sampledata.txt','text/plain','data_file');
$data = array('test_file' => $cfile);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
?>
和我的views.py
版本的flask服务器代码:
@app.route('/transaction', methods=['POST'])
def read_file():
if request.headers['Content-Type'] == 'text/plain':
return "Text Message: " + request.data
然后启动flask服务器,它显示(run.py是FLASK_APP
变量):
* Serving Flask app "run"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
接下来,当我在浏览器中输入/localhost/testscript.php
时,我得到以下错误:
发布于 2017-08-09 13:43:39
您正在端口5000上运行python服务,您需要将CURL上要使用的URL更改为:
$url='http://127.0.0.1:5000/transaction'
https://stackoverflow.com/questions/45591839
复制相似问题