2 # -*- coding: utf-8 -*-
4 otptools base module - retain session/login info
7 OTP_LOGIN_URL = "http://slashdot.jp/login.pl"
8 OTP_LOGIN_HOST = "slashdot.jp"
9 OTP_LOGIN_PATH = "/login.pl"
14 "returnto":"http://slashdot.jp",
20 OTP_LIST_PATH = "/admin.pl"
22 BROWSER = "Mozilla/5.0 (Windows; U; Windows NT 6.0; ja; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7 (.NET CLR 3.5.30729) "
27 import urllib, urllib2
30 class otptools(object):
32 Open Tech Press management library core module.
35 def __init__(self, path_cookie, login_name="", login_password=""):
37 @param path_cookie: path of file which cookie's information stored.
38 @type path_cookie: stinrg
40 @param login_name: OTP's login name for use.
41 @type login_name: string
43 @param login_password: OTP's login password.
44 @type login_password: string
46 self.path_cookie = path_cookie
47 self.unickname = login_name
48 self.upasswd = login_password
55 login_param = copy.deepcopy(OTP_LOGIN_PARAM)
56 login_param["unickname"] = self.unickname
57 login_param["upasswd"] = self.upasswd
59 for item in login_param:
60 print "%s > %s" % (item, login_param[item])
62 encoded_data = urllib.urlencode(login_param)
66 "User-Agent": BROWSER,
67 "Content-type": "application/x-www-form-urlencoded",
68 "Accept": "text/plain",
71 # obj = urllib.urlopen(OTP_LOGIN_URL, encoded_data)
74 obj = httplib.HTTPConnection(OTP_LOGIN_HOST)
75 obj.request("POST", OTP_LOGIN_PATH, encoded_data, headers)
76 resp = obj.getresponse()
77 headers = resp.getheaders()
82 # for header in headers:
83 # if header[0] == "set-cookie":
84 # str_cookie = header[1]
89 # self.cookie = str_cookie
92 def save_cookie(self):
93 file_obj = open(self.path_cookie, "w")
94 file_obj.write(self.cookie)
97 def load_cookie(self):
99 load session cookie from file.
101 file_obj = open(self.path_cookie, "r")
102 self.cookie = file_obj.readline()
107 Access admin.pl and retrieve stories list.
110 "User-Agent": BROWSER,
111 "Content-type": "application/x-www-form-urlencoded",
112 "Accept": "text/plain",
113 "Cookie": self.cookie,
115 obj = httplib.HTTPConnection(OTP_LOGIN_HOST)
116 obj.request("POST", OTP_LIST_PATH, "", headers)
117 resp = obj.getresponse()
118 headers = resp.getheaders()