Joined
·
6,523 Posts
Hey fellow programmers. I am trying to write a program in python that monitors my subscription feed and sends me a text when it changes but I need to log on to view it. I have google'd around and every solution I have come across doesn't seem to work with OCN so if anyone could give me any insight or advice, it would be greatly appreciated. Here is what I have so far (still a work in progress):
Code:
The code above works perfectly with pages that you don't need to be logged on to view so I only need help passing login credentials to OCN from python. Here is a bit of code that I found and tried but it didn't work. I don't have much experience at all with urllib and frankly most networking practices within python so I'm stuck on where to go from here.
Code:
Thanks for any and all advice!
Code:
Code:
#!/usr/bin/env python
import urllib, time, os, smtplib
url = "http://www.overclock.net/users/subscriptions/index/view/activity"
source = urllib.urlopen(url).read()
new_source=source
counter=0
flag=True
while new_source==source:
if flag:
time.sleep(5)
try:
new_source = urllib.urlopen(url).read()
except IOError:
print "Error: cannot read URL"
flag=False
continue
counter+=1
flag=True
fromaddr = '[email protected]'
toaddrs = "[email protected]"
body = "New Item Available!"
msg = 'From: [email protected]\n\n' + body
smtp_server = 'email-smtp.us-east-1.amazonaws.com'
smtp_username = '*****'
smtp_password = '*****'
smtp_port = '587'
smtp_do_tls = True
server = smtplib.SMTP(
host = smtp_server,
port = smtp_port,
timeout = 10
)
server.starttls()
server.ehlo()
server.login(smtp_username, smtp_password)
server.sendmail(fromaddr, toaddrs, msg)
Code:
Code:
#!/usr/bin/env python
import urllib2
import sys
import re
import base64
from urlparse import urlparse
theurl = 'http://www.overclock.net/users/subscriptions/index/view/activity'
# if you want to run this example you'll need to supply
# a protected page with your username and password
username = 'myOCNusername'
password = 'myOCNpassword'
req = urllib2.Request(theurl)
try:
handle = urllib2.urlopen(req)
except IOError, e:
# here we *want* to fail
pass
else:
# If we don't fail then the page isn't protected
print "This page isn't protected by authentication."
sys.exit(1)
if not hasattr(e, 'code') or e.code != 401:
# we got an error - but not a 401 error
print "This page isn't protected by authentication."
print 'But we failed for another reason.'
sys.exit(1)
authline = e.headers['www-authenticate']
# this gets the www-authenticate line from the headers
# which has the authentication scheme and realm in it
authobj = re.compile(
r'''(?:\s*www-authenticate\s*:)?\s*(\w*)\s+realm=['"]([^'"]+)['"]''',
re.IGNORECASE)
# this regular expression is used to extract scheme and realm
matchobj = authobj.match(authline)
if not matchobj:
# if the authline isn't matched by the regular expression
# then something is wrong
print 'The authentication header is badly formed.'
print authline
sys.exit(1)
scheme = matchobj.group(1)
realm = matchobj.group(2)
# here we've extracted the scheme
# and the realm from the header
if scheme.lower() != 'basic':
print 'This example only works with BASIC authentication.'
sys.exit(1)
base64string = base64.encodestring(
'%s:%s' % (username, password))[:-1]
authheader = "Basic %s" % base64string
req.add_header("Authorization", authheader)
try:
handle = urllib2.urlopen(req)
except IOError, e:
# here we shouldn't fail if the username/password is right
print "It looks like the username or password is wrong."
sys.exit(1)
thepage = handle.read()
