Soumen Roy
asked on
Python script not running for Raspberry Pi 3
Hi,
I am trying to run following python script in my Raspberry Pi. What I want to do is to extract Public IP Address of my Raspberry. But I am getting NULL. Please advise.
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -------
from urllib.request import urlopen
import re
#Setup Where I will get my IP Address
url = 'https://whatismyipaddress.com/'
print ("Our Choosen IP Address Service Is: ", url)
#Open-up the IP url and Take Away the IP Address
request = urlopen(url).read().decode ('utf-8')
#Extract the IP Address Only
ourIP=re.findall("\d{1,3}\ .\d{1,3}\. \d{1,3}\.\ d{1,3}", request)
ourIP = str(ourIP)
print ("Our IP Address is: ", ourIP)
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---
Output, what I am getting:
Our Choosen IP Address Service Is: https://whatismyipaddress.com/
Our IP Address is: []
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---
Regards,
Soumen
I am trying to run following python script in my Raspberry Pi. What I want to do is to extract Public IP Address of my Raspberry. But I am getting NULL. Please advise.
--------------------------
from urllib.request import urlopen
import re
#Setup Where I will get my IP Address
url = 'https://whatismyipaddress.com/'
print ("Our Choosen IP Address Service Is: ", url)
#Open-up the IP url and Take Away the IP Address
request = urlopen(url).read().decode
#Extract the IP Address Only
ourIP=re.findall("\d{1,3}\
ourIP = str(ourIP)
print ("Our IP Address is: ", ourIP)
--------------------------
Output, what I am getting:
Our Choosen IP Address Service Is: https://whatismyipaddress.com/
Our IP Address is: []
--------------------------
Regards,
Soumen
ASKER
Oh ho,
Okay. Got your point. I need to get my public ip in such a way. Can you suggest any site where I can collect that with such process? Or can I access Google service for this? If that is possible, can you please suggest that code?
Regards,
Soumen
Okay. Got your point. I need to get my public ip in such a way. Can you suggest any site where I can collect that with such process? Or can I access Google service for this? If that is possible, can you please suggest that code?
Regards,
Soumen
ASKER
I mean, is there any free service for such thing?
Regards,
Soumen
Regards,
Soumen
The myexternalip site is what you can use, a simpler python implementation (from that site) that is working:
import urllib
url = 'http://myexternalip.com/raw'
myip = urllib.urlopen(url).read()
print myip
ASKER
@Gerwin Jansen:
Thank you for your guidance, but it is not working directly in Pi. I have changed as follows:
from urllib.request import urlopen
import re
#Setup Where I will get my IP Address
url = 'http://myexternalip.com/raw'
print ("Our Choosen IP Address Service Is: ", url)
#Open-up the IP url and Take Away the IP Address
myip = urlopen(url).read()
#Extract the IP Address Only
#ourIP=re.findall("\d{1,3} \.\d{1,3}\ .\d{1,3}\. \d{1,3}", request)
#ourIP = str(ourIP)
print ("Our IP Address is: ", myip)
-------------------------- ---------- ---------- ---------- ---------- --------
But still a little problem is there. I am getting the ip as:
Our Choosen IP Address Service Is: http://myexternalip.com/raw
Our IP Address is: b'xxx.xx.xxx.xxx\n'
How to remove "b" and "\n" before and after that IP address? Any suggestion?
Regards,
Soumen
Thank you for your guidance, but it is not working directly in Pi. I have changed as follows:
from urllib.request import urlopen
import re
#Setup Where I will get my IP Address
url = 'http://myexternalip.com/raw'
print ("Our Choosen IP Address Service Is: ", url)
#Open-up the IP url and Take Away the IP Address
myip = urlopen(url).read()
#Extract the IP Address Only
#ourIP=re.findall("\d{1,3}
#ourIP = str(ourIP)
print ("Our IP Address is: ", myip)
--------------------------
But still a little problem is there. I am getting the ip as:
Our Choosen IP Address Service Is: http://myexternalip.com/raw
Our IP Address is: b'xxx.xx.xxx.xxx\n'
How to remove "b" and "\n" before and after that IP address? Any suggestion?
Regards,
Soumen
Did you try the code from my last comment? If you did, then show the error you get, 'it is not working' is not something we can help you with.
The last sample code is working on my Raspberry and it is giving back the IP address without the b and \n
The last sample code is working on my Raspberry and it is giving back the IP address without the b and \n
ASKER
@Gerwin Jansen:
Yes, When I tried your code, I am getting following error.
Then I replaced as "print (myip)". Now I am getting the following msg.
So, I replace that code as given in my last comment. It worked, only to remove "b" and "\n" from the returned string.
b'xxx.xx.xxx.xxx\n'
May be I am doing something wrong. I am now using the code I have given my last comment.
Please advise.
Regards,
Soumen
Yes, When I tried your code, I am getting following error.
Then I replaced as "print (myip)". Now I am getting the following msg.
So, I replace that code as given in my last comment. It worked, only to remove "b" and "\n" from the returned string.
b'xxx.xx.xxx.xxx\n'
May be I am doing something wrong. I am now using the code I have given my last comment.
Please advise.
Regards,
Soumen
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Access Denied (AK1). Contact support@whatismyipaddress.
If you try whatismyip.com you get the same. That site says:
But http://myexternalip.com is allowing you to get your external address using a script. Try that instead.