Python Cookbook 2Nd Edition Jun 1002005 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Python Cookbook 2Nd Edition Jun 1002005 [Electronic resources] - نسخه متنی

David Ascher, Alex Martelli, Anna Ravenscroft

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید


Recipe 14.10. Finding an Internet Explorer Cookie


Credit: Andy McKay


Problem


You need to find a specific
IE cookie.


Solution


Cookies that your browser has downloaded contain potentially useful
information, so it's important to know how to get at
them. With Internet Explorer (IE), one simple approach is to access
the registry to find where the cookies are, then read them as files.
Here is a module with the function you need for that purpose:

import re, os, glob
import win32api, win32con
def _getLocation( ):
"" Examines the registry to find the cookie folder IE uses ""
key = r'Software\Microsoft\WindowsCurrentVersion\Explorer\Shell Folders'
regkey = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, key, 0,
win32con.KEY_ALL_ACCESS)
num = win32api.RegQueryInfoKey(regkey)[1]
for x in range(num):
k = win32api.RegEnumValue(regkey, x)
if k[0] == 'Cookies':
return k[1]
def _getCookieFiles(location, name):
"" Rummages through cookie folder, returns filenames including `name'.
`name' is normally the domain, e.g 'activestate' to get cookies for
activestate.com (also e.g. for activestate.foo.com, but you can
filter out such accidental hits later). ""
filemask = os.path.join(location, '*%s*' % name)
return glob.glob(filemask)
def _findCookie(filenames, cookie_re):
"" Look through a group of files for a cookie that satisfies a
given compiled RE, returning first such cookie found, or None. ""
for file in filenames:
data = open(file, 'r').read( )
m = cookie_re.search(data)
if m: return m.group(1)
def findIECookie(domain, cookie):
"" Finds the cookie for a given domain from IE cookie files ""
try:
l = _getLocation( )
except Exception, err:
# Print a debug message
print "Error pulling registry key:", err
return None
# Found the key; now find the files and look through them
f = _getCookieFiles(l, domain)
if f:
cookie_re = re.compile('%s\n(.*?)\n' % cookie)
return _findCookie(f, cookie_re)
else:
print "No cookies for domain (%s) found" % domain
return None
if _ _name_ _=='_ _main_ _':
print findIECookie(domain='kuro5hin', cookie='k5-new_session')


Discussion


While Netscape cookies are in a text file, IE keeps cookies as files
in a directory, and you need to access the registry to find which
directory that is. To access the Windows registry, this recipe uses
the PyWin32 Windows-specific Python extensions; as
an alternative, you could use the _winreg module
that is part of Python's standard distribution for
Windows. This recipe's code has been tested and
works on IE 5 and 6.

In the recipe, the _getLocation function accesses
the registry and finds and returns the directory that IE is using for
cookie files. The _getCookieFiles function receives
the directory as an argument and uses standard module
glob to return all filenames in the directory
whose names include a particular requested domain name. The
_findCookie function opens and reads all such files
in turn, until it finds one whose contents satisfy a compiled regular
expression that the function receives as an argument. It then returns
the substring of the file's contents corresponding
to the first parenthesized group in the regular expression, or
None when no satisfactory file is found. As the
leading underscore in the names indicates, these are all internal
functions, used only as implementation details of the only function
this module is meant to expose, namely findIECookie,
which uses the other functions to locate and return the value of a
specific cookie for a given domain.

An alternative to this recipe would be to write a Python extension,
or use calldll or ctypes, to
access the InternetGetCookie API function in
Wininet.DLL, as documented on MSDN (Microsoft
Developer Network).


See Also


The Unofficial Cookie FAQ (http://www.cookiecentral.com/faq/) is
chock-full of information on cookies; documentation for
win32api and win32con in
PyWin32 (http://starship.python.net/crew/mhammond/win32/Downloadsl)
or ActivePython (http://www.activestate.com/ActivePython/);
Windows API documentation available from Microsoft (http://msdn.microsoft.com); Mark Hammond and
Andy Robinson, Python Programming on Win32
(O'Reilly); calldll is available
at Sam Rushing's page (http://www.nightmare.com/~rushing/dynwin/);
ctypes is at http://sourceforge.net/projects/ctypes.

/ 394