How to send http requests to flask server

2019-03-04 13:23发布

When my flask application is started, it says

Running on http://0.0.0.0:80/

How do I send http requests to this server?

I tried

telnet 0.0.0.0 80

But it says:

Trying 0.0.0.0...
telnet: connect to address 0.0.0.0: Invalid argument

I used the request module:

r = requests.get('http://0.0.0.0:80')

It says the same result

 Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/requests/api.py", line 55, in get
    return request('get', url, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 288, in request
    resp = self.send(prep, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)
  File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 383, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/adapters.py", line 206, in send
    raise ConnectionError(sockerr)
requests.exceptions.ConnectionError: [Errno 22] Invalid argument

The output of netstat is given below:

# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:http            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:domain          0.0.0.0:*               LISTEN
tcp6       0      0 [::]:domain             [::]:*                  LISTEN
udp        0      0 0.0.0.0:domain          0.0.0.0:*
udp        0      0 0.0.0.0:bootps          0.0.0.0:*
udp6       0      0 [::]:domain             [::]:*

2条回答
Bombasti
2楼-- · 2019-03-04 13:45

"0.0.0.0" is just a special address to indicte the server is listening to all IPs on its interface.

You should still access it using your compuer local address: "127.0.0.1", or "localhost".

查看更多
Evening l夕情丶
3楼-- · 2019-03-04 13:47

0.0.0.0 means "all IPv4 addresses on the local machine". It is a meta-address which is non-routable.

If you want to access the server locally, i.e. client on the same machine as the server, either use the IP address 127.0.0.1 (loopback Internet protocol) or the equivalent named domain name (localhost)

r = requests.get('http://127.0.0.1:80')
r = requests.get('http://localhost:80')

If you are accessing the server from a remote machine (on the same subnet), you can connect to your server through the IP address of server (assigned by your router/gateway) running your service

If you are accessing the server from a remote machine otherwise, you can connect to your server through the IP address of your router. You need to set up Virtual servers by appropriate port forwarding.

查看更多
登录 后发表回答