Reputation:
Stackoverflow does not seem to have something about public IP addresses in python, so I am asking a question. I have a raspberry pi, and upon start up, I want it to check for its global ip address and its private ip address, and if possible, post it to Twitter. Is there any way to do all of this? I know about the twython
module, but I have never used it before.
Upvotes: 2
Views: 899
Reputation: 19264
First, you have to go to twitter.com and create an account, and then dev.twitter.com/apps to create your app, from which you get your APP_KEY
, APP_SECRET
, OAUTH_TOKEN
, and OAUTH_TOKEN_SECRET
. Remember to NEVER share this information, for it will cause anyone to automatically be able to tweet from your twitter account. To get your public ip address, type curl ifconfig.me
, and it will print it. However, you cannot do this in a python program. To go around this, use a module like pycurl
or go to http://docs.python-requests.org/en/latest/index.html to learn more. To get your private IP address, use this:
import socket
socket.gethostbyname(socket.gethostname())
Here is the twitter documentation:
from twython import Twython
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
twitter.update_status(status='See how easy using Twython is!')
You can change update_status
to print your variables for the public and private IP address.
Here is how to run a program on a Pi when starting up, type this into your shell:
cd /
sudo crontab -e
This will nano
the file crontab. At the very end of this file, add @reboot /home/pi/myscript.py
, and on startup, it will run the program. Hope this helps!
Upvotes: 1