[Newbie Q on String & List Manipulation]

Matthew matthew at coredumps.net
Thu Apr 15 06:47:06 EDT 2004


Hello All,

    today is the first day i try to programming in Python,
    my assignment is, write a silly script that probably
    will run a few times a day to check if the Gmail services
    is ready or not. ;)

    however, i encountered some problem when playing with the
    list and string.

    i'm using Python 2.2.2 on Redhat. if i write something like:

        a = "one"
        b = "two"
        a += b
        print a

    i will get:

        onetwo

    ok, seems quite ok, however, not sure why it doesn't work on
    my silly Gmail script (pls refer to my script belows):

        for item in thecookies:
            mycookies += item

        print mycookies

    i have exactly 4 items in the "thecookies" list, however, when
    printing out "mycookies", it just show the last item (in fact,
    seems the 4 items have been overlapped each others).

    could somebody pls kindly take a look at my silly script and
    gimme some advise?

    thanks very much in advance! :)

---
matthew




import re
import string
import sys
import urllib

user = "da at email.address"
pswd = "dapassword"

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()

print thecookies

mycookies = ""

for item in thecookies:
        mycookies += item

print mycookies

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

sys.exit()



More information about the Python-list mailing list