Du bist hier: Snippet-Verzeichnis » Python (121)
Sprache:

cookiemonster

Sprache: English
Programmiersprache: Python
Veröffentlicht von: jmcvetta [nicht registriert]
Letzte Änderung: 15.05.2006
Aufrufe: 1073


Beschreibung

Eats all (netscape) cookies except those you tell it not to eat. Useful if you want cookies enabled, but don't want people gathering too much information about you.

Code

1 #!/usr/local/bin/python 2 ''' 3 Cookie Monster 4 5 Eats all the cookies you don't want. (A considerable improvement over Sesame 6 Street's Cookie Monster, who would eat /all/ the cookies he could get his 7 hands on.) 8 9 ============================================================================== 10 11 The latest version of this software may be obtained at: 12 13 http://larkin.dyndns.org/software 14 15 Other software by the same author may be found at: 16 17 http://larkin.dyndns.org/software 18 19 The author may be contacted at this address: 20 21 mcvetta.3@osu.edu 22 23 Users are highly encouraged to send their comments, suggestions, cash 24 contributions, and/or snide remarks. 25 26 This is free software. However, if you feel an unavoidable urge to pay for it, 27 please make a donation to the ACLU <www.aclu.org> or Solidarity 28 <www.solidarity-us.org>. 29 30 ============================================================================== 31 32 $Id: cookiemonster.py,v 1.4 2001/04/17 22:18:06 jmcvetta Exp $ 33 34 ============================================================================== 35 36 Copyright (c) 2001 Jason R. McVetta 37 38 Permission is hereby granted, free of charge, to any person obtaining a copy of 39 this software and associated documentation files (the "Software"), to deal in 40 the Software without restriction, including without limitation the rights to 41 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 42 of the Software, and to permit persons to whom the Software is furnished to do 43 so, subject to the following conditions: 44 45 The above copyright notice and this permission notice shall be included in all 46 copies or substantial portions of the Software. 47 48 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 49 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 50 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 51 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 52 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 53 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 54 SOFTWARE. 55 ''' 56 #== DEFAULTS ================================================================= 57 cookiefile = '~/.netscape/cookies' 58 rcfile = '~/.cookiemonsterrc' 59 60 #== MAIN ===================================================================== 61 import re, string, os 62 63 # There should be command line options here to specify cookiefile and/or 64 # rcfile. 65 66 cookiefile = os.path.expanduser(cookiefile) 67 rcfile = os.path.expanduser(rcfile) 68 69 for file in cookiefile, rcfile: # Make sure the needed files exist 70 if not os.path.isfile(file): 71 import sys 72 sys.exit('Cookiemonster: Fatal Error!\n File not found: "%s".' \ 73 % file) 74 75 f = open(rcfile, 'r') 76 rctext = f.read() 77 f.close() 78 del f 79 80 valid_domain = re.compile(r''' 81 [.]? # It's okay if the domain begins with a dot 82 (\w+ # Nth-level domain name (non-top) 83 [.] # Dot 84 )+ # Repeat at least once 85 (com|edu|net|org| 86 gov|mil|uk) # Top-level domain (US & UK only, unless there's need) 87 $ # End of the line 88 ''', re.VERBOSE) 89 90 domainlist = [] 91 rclines = string.split(rctext, '\n') 92 # For some reason string.split() puts a blank element at the end of the tuple. 93 if '' == rclines[ len(rclines) - 1 ]: 94 rclines = rclines[ : len(rclines) - 1 ] 95 for item in rclines: 96 if valid_domain.match(item): 97 item = string.replace(item, '.', '[.]') 98 domainlist.append(item) 99 else: 100 print ' %s: "%s" is not a valid domain.' % (rcfile, item) 101 102 domainlist = string.join(domainlist, '|') 103 domainlist = '(' + domainlist + ')$' 104 noeat = re.compile(domainlist) 105 106 f = open(cookiefile, 'r') 107 cookielist = f.read() 108 f.close() 109 del f 110 111 comments = [] # Comments, which begin with a '#' 112 goodcookies = [] # Cookies we will be keeping 113 cookielist = string.split(cookielist, '\n') 114 for cookie in cookielist: 115 if not cookie: # Skip blank lines 116 continue 117 if '#' == cookie[0]: 118 comments.append(cookie) 119 continue 120 try: 121 index = string.index(cookie, '\t') 122 except: 123 print 'Invalid cookie format:' 124 print ' %s' % cookie 125 continue 126 cookiedomain = cookie[ : index] 127 if noeat.match(cookiedomain): 128 goodcookies.append(cookie) 129 130 comments = string.join(comments, '\n') 131 goodcookies = string.join(goodcookies, '\n') 132 133 f = open(cookiefile, 'w+') 134 f.write(comments) 135 f.write('\n\n') 136 f.write(goodcookies) 137 f.close() 138 del f 139

Noch kein Kommentar vorhanden

Dieses Snippet kommentieren

Name *  

E-Mail (wird nicht angezeigt) *    

Website  

Kommentar *  

Sicherheitscode Sicherheitscode *    

RSS