Skip to content

Instantly share code, notes, and snippets.

@damienix
Created January 14, 2011 19:28
Show Gist options
  • Save damienix/780083 to your computer and use it in GitHub Desktop.
Save damienix/780083 to your computer and use it in GitHub Desktop.
Client for scrobbling uptime on uptime.szamanie.pl
#!/usr/bin/python2
# Title : uptime-client
# Date : 2011-01-10
# Modified : 2011-01-13
# Author : Damian Skrodzki <damienix1@gmail.com>
# Version : 0.1
# Description : Client designed for sending data to server for uptime measurement.
# TODO : -
import urllib2
import cookielib
import commands
import time
import sys
from multiprocessing import Pool
import ClientForm
import getpass
class UptimeClient:
"""
Class representing client for scrobbling your uptime.
"""
def __init__(self, *args):
if len(args) > 0:
self.username = args[0]
self.password = args[1]
else:
self.username = raw_input("login: ")
self.password = getpass.getpass("password: ")
self.host = "http://uptime.szamanie.pl/"
# self.host = "http://127.0.0.1:8000"
self.url = "%s/user/login/" % self.host
self.key = abs(hash(commands.getstatusoutput('echo $(uname -pimson && hostname)')))
cookiejar = cookielib.LWPCookieJar()
cookiejar = urllib2.HTTPCookieProcessor(cookiejar)
# debugger = urllib2.HTTPHandler(debuglevel=1)
opener = urllib2.build_opener(cookiejar)
urllib2.install_opener(opener)
self.login()
self.start_process()
def login(self):
response = urllib2.urlopen(self.url)
forms = ClientForm.ParseResponse(response, backwards_compat=False)
# forms[0] is 'GET', forms[1] is 'POST'
form = forms[0]
try:
form['username'] = self.username
form['password'] = self.password
except Exception, e:
print 'The following error occured: \n"%s"' % e
print
print 'A good idea is to open a browser and see if you can log in from there.'
print 'URL:', self.url
raw_input()
exit()
# print form
self.stop()
self.page = urllib2.urlopen(form.click('login')).read()
def stop(self):
try:
print "Trying to terminate current uptime."
# Stop current Uptime
uri="%s/uptimes/stop/" % self.host +str(self.key)+"/"
# print uri
rq = urllib2.Request(url=uri)
rs = urllib2.urlopen(rq)
print "Uptime stopped."
except Exception, e:
print "Nothing to stop"
def ping(self, request):
urllib2.urlopen(request)
def process(self):
uri = "%s/uptimes/start/" % self.host+str(self.key)+"/"
print uri
rq = urllib2.Request(url=uri)
print "Start pinging... (Ctrl+C to terminate)"
while True:
try:
self.ping(rq)
except Exception, e:
print "Interrupted: %s" % e
sys.exit(1)
time.sleep(45)
def start_process(self):
p = Pool(processes=1)
try:
p.map(self.process(), range(1))
p.close()
except KeyboardInterrupt:
p.terminate()
print "You terminated pinging!"
self.stop()
sys.exit(1)
finally:
p.join()
def my_hosts(self):
# pattern = re.compile('.*-color:.*;">(.*)</li>')
# friends_online = pattern.findall(self.page)
print self.page
#UC = UptimeClient('test', 'test')
UC = UptimeClient()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment