当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Unable to access Flask web app from php curl

问题描述:

I have a Flask app, with a basic function, where I have exposed app.run() to a public ip, so that it is accessible from an external server;[ using Flask - Externally Visible Dev Server ]

@app.route('/')

def hello_world():

return 'Hello World!'

if __name__ == '__main__':

app.run(host = '0.0.0.0', port = 8080)

The curl request I have written in my php code is:

$signed_url = "http://my-ip-address:8080/";

$ch = curl_init($signed_url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HEADER, 0);

$data= curl_exec($ch);

echo $data;

I can do a curl request :

curl http://my-ip-address:8080/

from command line. However, when the curl request is embedded within my PHP code, it gives me an error "Connection refused".

Kindly help!

网友答案:

If the PHP code is on another server, but your command line cURL request is on the same server, then you aren't comparing apples to apples.

Two things that might be wrong:

  1. Your Flask server has a firewall that doesn't allow external connections.
  2. You are connecting using an private network IP address rather than a public IP address.

For now your PHP code looks correct, so I would narrow down the problem a little bit. Ignore that PHP code and try to connect using cURL on the command line from the same server you are running your PHP code on.

网友答案:

try to set your port with curl options like this:

curl_setopt($ch, CURLOPT_PORT, 8080);

so your signed url will be:

$signed_url = "http://my-ip-address";
分享给朋友:
您可能感兴趣的文章:
随机阅读: