Inspired by http://drupal4hu.com/node/266 in started to write a small python script which fills out the cisco "network authentication system" on my home.
Therefore the script has to fill out a form and click a Login button. My connection drops every 8 hours so i thought it would be cool to automatically login every 8-N hours so i don't have to do it automatically any more.
So here is a small script
Inspired from http://drupal4hu.com/node/266
These lines will get us the modules we need.
from PyQt4.QtCore import QUrl, SIGNAL
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QTimer
from PyQt4.QtCore import QObject
from PyQt4.QtWebKit import QWebPage, QWebView
class Scrape(QApplication):
def init(self):
# Apparently there are a number of versions of this init and PyQT
# figures out which you want based on the number of arguments. So pass
# in one argument but we do not need anything really, so None.
super(Scrape, self).init(None)
# Create a QWebView instance and store it.
self.webView = QWebView()
# Connect our loadFinished method to the loadFinished signal of this new
# QWebView.
self.webView.loadFinished.connect(self.loadFinished)
def load(self, url):
# In the init we stored a QWebView instance into self.webView so
# we can load a url into it. It needs a QUrl instance though.
self.webView.load(QUrl(url))
def loadFinished(self):
self.webView.loadFinished.disconnect(self.loadFinished)
# Set up a qtimer to execute inputData every second
self.ctimer = QTimer()
minutes = 30
seconds = 60 * minutes
milliseconds = seconds * 60
self.ctimer.start(1000)
self.ctimer.setInterval(1000)
#QObject.connect(self.ctimer, SIGNAL("timeout()"), self.inputData)
self.ctimer.timeout.connect(self.inputData)
# We are inside a QT application and need to terminate that properly.
#self.exit()
def inputData(self):
# We landed here because the load is finished. Now, load the root document
# element. It'll be a QWebElement instance. QWebElement is a QT4.6
# addition and it allows easier DOM interaction.
documentElement = self.webView.page().currentFrame().documentElement()
# Find login and username/password
field_login = documentElement.findFirst('input[name="Login"]')
field_username = documentElement.findFirst('input[name="username"]')
field_password = documentElement.findFirst('input[name="password"]')
# Print it out.
username = '123'
password = '123'
# Set the username and password.
field_username.setAttribute('value', username)
field_password.setAttribute('value', password)
self.webView.loadFinished.connect(self.LoginResult)
field_login.evaluateJavaScript('this.click()')
def LoginResult(self):
print "hallo"
print unicode(self.webView.page().currentFrame().documentElement().toOuterXml())
# TODO Figure out what do to.
Instantiate our class.
myScrape = Scrape()
Load the Netaccess page.
myScrape.load('http://10.199.240.1/netaccess/loginuser.html')
Start the QT event loop.
myScrape.exec_()
Comments
Anonymous (not verified)
3. October 2010 - 9:56
Permalink
thanks a lot
thanks a lot
Add new comment