Solved

Python script not running for Raspberry Pi 3

Posted on 2016-07-30
8
73 Views
Last Modified: 2016-09-28
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
0
Comment
Question by:Soumen Roy
  • 4
  • 4
8 Comments
 
LVL 37

Expert Comment

by:Gerwin Jansen
Comment Utility
You are not allowed to access that site using Python, if you look at the request you will see this:

Access Denied (AK1).  Contact support@whatismyipaddress.com

If you try whatismyip.com you get the same. That site says:

What Is My IP API
If you need data returned to you via script, we provide an API section where you'll be able to call each of our tools and get the output in txt, json, or xml. You'll need to sign up for our Gold Membership to gain access to our API. Read our API Quick Start Guide to get the API up and running and working for you.

But http://myexternalip.com is allowing you to get your external address using a script. Try that instead.
0
 

Author Comment

by:Soumen Roy
Comment Utility
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
0
 

Author Comment

by:Soumen Roy
Comment Utility
I mean, is there any free service for such thing?

Regards,
Soumen
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
Comment Utility
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

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Soumen Roy
Comment Utility
@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
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
Comment Utility
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
0
 

Author Comment

by:Soumen Roy
Comment Utility
@Gerwin Jansen:

Yes, When I tried your code, I am getting following error.

1.PNG
Then I replaced as "print (myip)". Now I am getting the following msg.

2.PNG
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
0
 
LVL 37

Accepted Solution

by:
Gerwin Jansen earned 500 total points
Comment Utility
You are running a GUI on your Raspberry?

Your question states that you want to run a python script.

Anyway, your program can't find 'urlopen' - do you have "import url lib"?

RPI:/tmp # cat getip.py
import urllib
url = 'http://myexternalip.com/raw'
myip = urllib.urlopen(url).read()
print (myip)
RPI:/tmp # python getip.py 
www.xxx.yyy.zzz

RPI:/tmp # 

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now