开发者

cookielib and form authentication woes in Python

开发者 https://www.devze.com 2022-12-15 18:31 出处:网络
InstaMapper is a GPS tracking service that updates the device\'s position more frequently when the device is being tracked live on the InstaMapper webpage.I\'d like to have this happen all the time so

InstaMapper is a GPS tracking service that updates the device's position more frequently when the device is being tracked live on the InstaMapper webpage. I'd like to have this happen all the time so I thought I'd write a python script to login to my account and access the page periodically.

import urllib2, urllib, cookielib

cj = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 
urllib2.install_opener(opener)

params = urllib.urlencode(dict(username_hb='user', password_hb='hunter2')) 
opener.open('http://www.instamapper.com/fe?actio开发者_开发百科n=login', params)
if not 'id' in [cookie.name for cookie in cj]:
    raise ValueError, "Login failed"

# try secured page
resp = opener.open('http://www.instamapper.com/fe?page=track&device_key=abc')
print resp.read()
resp.close()

The ValueError is raised each time. If I remove this and read the response, the page thinks I have disabled cookies and blocks access to that page. Why isn't cj grabbing the InstaMapper cookie?

Are there better ways to make the tracking service think I'm viewing my account constantly?


action=login is part of the parameters, and should be treated accordingly:

params = urllib.urlencode(dict(action='login', username_hb='user', password_hb='hunter2')) 
opener.open('http://www.instamapper.com/fe', params)

(Also, this particular username/password combination is invalid, I assume, that you actually use a valid username and password in your actual code, otherwise the login fails corretly.)


Have you looked at whether there is a cookie specifically designed to foil your attempts? I suggest using Wireshark or other inspector to see if there is a cookie that changes (via javascript, etc) when you manually log in.

(Ethical note: You may be violating the terms of service and incurring much more cost to the company than you are paying for. I used to run a service like this and every additional/unplanned location update was between $0.01 - $0.05 but I'm sure its come down.)

0

精彩评论

暂无评论...
验证码 换一张
取 消