Sending GET request for an IP address
Question:
I need to send a request to a web server of mine to start a stream. The web server is located at 0.0.0.0
(of course I can change the address).
How can I send a “GET” request to that server?
I already tried using httplib
or urllib2
or 3
and they seem not to work with IP address.
I know that a local DNS server will map the address to a url, but that is not the goal to set up the server every time I want to execute the code in a new network.
Thank a lot.
Answers:
It doesn’t work because you probably haven’t included the web protocol to use (i.e. HTTP or HTTPS). Try it like this
import urllib2
urllib2.urlopen('http://0.0.0.0')
You could use requests
:
requests.get('http://0.0.0.0')
or even better
s = requests.Session()
s.get('http://0.0.0.0')
r = s.get('https://httpbin.org/cookies')
to keep the connection persistent, which is probably more like what you want.
See more about requests
sessions at http://docs.python-requests.org/en/master/user/advanced/
Or you could just convert the IP address to a hostname using
socket.gethostbyaddr(ip)
with urllib
to convert the IP address to a host name
I need to send a request to a web server of mine to start a stream. The web server is located at 0.0.0.0
(of course I can change the address).
How can I send a “GET” request to that server?
I already tried using httplib
or urllib2
or 3
and they seem not to work with IP address.
I know that a local DNS server will map the address to a url, but that is not the goal to set up the server every time I want to execute the code in a new network.
Thank a lot.
It doesn’t work because you probably haven’t included the web protocol to use (i.e. HTTP or HTTPS). Try it like this
import urllib2
urllib2.urlopen('http://0.0.0.0')
You could use requests
:
requests.get('http://0.0.0.0')
or even better
s = requests.Session()
s.get('http://0.0.0.0')
r = s.get('https://httpbin.org/cookies')
to keep the connection persistent, which is probably more like what you want.
See more about requests
sessions at http://docs.python-requests.org/en/master/user/advanced/
Or you could just convert the IP address to a hostname using
socket.gethostbyaddr(ip)
with urllib
to convert the IP address to a host name