Skip to content

Instantly share code, notes, and snippets.

@akuafif
Last active September 8, 2016 19:36
Show Gist options
  • Save akuafif/f1d7f81e79e0a130974b to your computer and use it in GitHub Desktop.
Save akuafif/f1d7f81e79e0a130974b to your computer and use it in GitHub Desktop.
[Python] Get Public IP and upload it to your DropBox account
#!/usr/bin/env python
# Useful if hosting something through a dynamic IP address
import dropbox
import urllib
import re
from datetime import datetime
from time import sleep
# Function to get External IP
def get_external_ip():
site = urllib.urlopen("http://myexternalip.com/raw").read()
grab = re.findall('([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)', site)
address = grab[0]
return address
# Get what to write and put it at DropBox
def upload_to_dropbox(toWrite):
# Get token at your app dropbox page -> Generate access token
# This part is to give access to the dropbox app folder
access_token = 'your_token'
client = dropbox.client.DropboxClient(access_token)
# Save it Locally
file = open('PublicIP.txt', 'w')
file.write(toWrite)
file.close()
# Upload it to the dropbox folder
f = open('PublicIP.txt', 'rb')
response = client.put_file('/PublicIP.txt', f, 1)
if __name__ == '__main__':
# Set a counter (for reference)
i = 1
# while(1). Will run till the end of time.
while(1):
# Create the string to be pass
toWrite = str(i) + ')As of ' + str(datetime.now()) + '\nExternal IP: ' + get_external_ip() + '\n'
# print to terminal if you want
# print(toWrite)
# Up load it to your dropbox folder
upload_to_dropbox(toWrite)
# Sleep(sec). Make the script stop for the time being
sleep(600)
i=i+1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment