[Newbie Q on String & List Manipulation]

Matthew matthew at coredumps.net
Fri Apr 16 01:02:45 EDT 2004


hello all,

thanks very much for you guys' replies. :)

i guess i already have properly indented the code,
and i also tried the string.join() method.

i changed the script a bit to test both the a+=b
and the string.join().

pls kindly take a look at the output and the script
below.

thanks in advance.

---
matthew


##########
# OUTPUT #
##########

=== the content of the "thecookies" list ===
['=zh_HK; Expires=Sat, 16-Apr-05 04:57:58 GMT; Path=/\r',
'Session=zh_HK\r', 'SID=AejhWhifAlWLXGi3lnBd3PiLeNUkoasZRP9kKXc0Es_o;Domain=.google.com;Path=/\r',
'GV=fbf1ad9eb8-4bbb676189c513f10bfa42556f57c6ac']

=== the content of the string: "mycookies" ===
GV=fbf1ad9eb8-4bbb676189c513f10bfa42556f57c6ac_o;Domain=.google.com;Path=/

=== use the string.join(): "".join(thecookies) ===
GV=fbf1ad9eb8-4bbb676189c513f10bfa42556f57c6ac_o;Domain=.google.com;Path=/

############
# Gmail.py #
############

#
# A Python script that would logging into the Gmail services and check
# if the message is still the "Sorry, Gmail is in limited test
mode..."
#
# Matthew Wong <matthew at coredumps.net> 2004-04-15
#

import re
import string
import sys
import urllib

user = "matthew at coredumps.net"
pswd = "maddog4096"

schm = "https://"
host = "www.google.com"
path = "/accounts/ServiceLoginBoxAuth"
qstr = {"service"  : "mail",                     \
	"continue" : "http://gmail.google.com/", \
	"Email"    : user,                       \
	"Passwd"   : pswd}

qstr = urllib.urlencode(qstr)

url  = schm + host + path + "?" + qstr

conn = urllib.urlopen(url)

headers  = conn.info().headers
response = conn.read()

thecookies = []

#
# extract all the Set-Cookie from the HTTP response header and put it
in thecookies
#

for header in headers:
	matches = re.compile("^Set-Cookie: (.*)$").search(header)
	if matches:
		thecookies.append(matches.group(1))

#
# make sure we've grep the SID or die
#

foundsessionid = 0

for item in thecookies:
	if re.compile("^SID").search(item):
		foundsessionid = 1
		break

if not foundsessionid:
	print "> Failded to retrieve the \"SID\" cookie"
	sys.exit()

#
# grep the GV cookie from the HTTP response or die
#

matches = re.compile("^\s*var cookieVal= \"(.*)\";.*",
re.M).search(response)

if matches:
	thecookies.append("GV=" + matches.group(1))
else:
	print "> Failed to retrieve the \"GV\" cookie"
	sys.exit()

#
# dump the content of the list: thecookies 
#

print "=== the content of the \"thecookies\" list ==="
print thecookies
print "\n"

#
# join the items in the "thecookies" list to
# the "mycookies" string by using the a += b
#

mycookies = ""

for item in thecookies:
	mycookies += item

print "=== the content of the string: \"mycookies\" ==="
print mycookies
print "\n"

#
# join the items in the "thecookies" list to
# the "mycookies" string by using the string.join()
#

print "=== use the string.join(): \"\".join(thecookies) ==="
print "".join(thecookies)
print "\n"

#
# still got many things to do right here...
#

sys.exit()



More information about the Python-list mailing list