简体   繁体   中英

Unable to curl POST textfile to python flask server on localhost

I have a functioning flask server on localhost and it's serving GET and POST requests normally when tested to serve simple python dictionaries. Now I'm trying to upload and read contents of a text file using the PHP script below (both file and script are in the same Windows directory):

<?  
    /*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);

?>

And my flask server code in views.py :

@app.route('/transaction', methods=['POST'])
def read_file():
    if request.headers['Content-Type'] == 'text/plain':
        return "Text Message: " + request.data

I then start the flask server, which shows (run.py is the FLASK_APP variable):

* Serving Flask app "run"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 

Next, when I enter /localhost/testscript.php in my browser, I get the following error:

在此处输入图片说明

您正在端口5000上运行python服务,您需要将要在CURL上使用的URL更改为:

$url='http://127.0.0.1:5000/transaction'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM