testing is currently certified at Journeyer level.

Name: Testy The Test Account
Member since: 2000-12-10 23:33:23
Last Login: N/A

FOAF RDF Share This

No personal information is available.

Recent blog entries by testing

Syndication: RSS 2.0

Testy is quite disappointed that lettuce-head is president.

#!/usr/bin/python

import getopt, sys, httplib, string, re, getpass, urllib

def usage():
  print "%s -p (--post-entry) <file>" % (sys.argv[0])

def extract_pass(header):
  '''
  Grab encrypted password from a "Set-Cookie" header.
  Example header:
    "Set-Cookie: id=jordan:32f/XCcPSGAMzsS5/ky/; path=/; Expires=Monday"
  Result:
    "32f/XCcPSGAMzsS5/ky/"
  '''
  # Grab the Set-Cookie: line.
  header = string.split(header, '\n')[2]
  
  return string.split(string.split(header, ':')[2], ';')[0]

def check_response(code, header):
  '''
  Check if HTTP headers were accepted by server.
  '''
  if code != 200:
    print 'Error sending headers -- %s' % (header)
    sys.exit()

def check_auth(header):
  '''
  Check if authentication failed.
  '''
  # Bad login or password.
  if string.find(header, 'Set-Cookie') == -1:
    print 'Error authenticating -- %s' % (header)
    sys.exit()

def extract_entry_number(html):
  '''
  Return the diary entry number from raw HTML code.
  '''
  # Find the line 'name=key value=N' where N is
  # the diary entry number and save it.
  entry_num = ''
  
  for line in html:
    if string.find(line, 'name=key value=') != -1:
      # Extract the number from the string
      entry_num = re.sub('\D', '', line)
      break

  return entry_num

def get_entry_number(userid, password):
  '''
  Get the diary entry number for "userid."  This number is
  necessary in order to post new diary entries. 
  '''
  hlink = httplib.HTTP('www.advogato.org')
  hlink.putrequest('GET', '/diary/ HTTP/1.0')
  hlink.putheader('Host', 'www.advogato.org')
  hlink.putheader('Cookie2', '$Version="1"')
  hlink.putheader('Cookie', 'id=%s:%s' % (userid, password))
  hlink.endheaders()
  errcode, errmsg, header = hlink.getreply()
  check_response(errcode, header)

  # Grab HTML.
  raw_html = string.split(hlink.getfile().read(), '\n')
  hlink.getfile().close()

  entry_num = ''
  entry_num = extract_entry_number(raw_html)
  
  if not entry_num:
    print 'Entry value for %s not found -- %s' % (userid, header)
    sys.exit()

  return entry_num

def get_entry(entry_file):
  '''
  Return the contents of `entry_file`, which contains
  the user`s entry.
  '''
  try:
    file_in = open(entry_file, 'r')
  except:
    print 'Error opening %s' % (entry_file)
    sys.exit()

  # Replace newlines with 

's, otherwise the encoded URL # will not be accepted. # entry = re.sub('\n', '

', file_in.read()) entry = file_in.read() return entry def post_entry(user_info, entry_file): ''' Post `entry_file` to advogato for the user described in `user_info.` ''' userid = user_info[0] password = user_info[1] urlencoded = urllib.urlencode({'u': userid, 'pass': password}) hlink = httplib.HTTP('www.advogato.org') hlink.putrequest('POST', '/acct/loginsub.html HTTP/1.0') hlink.putheader('Host', 'www.advogato.org') hlink.putheader('Content-type', 'application/x-www-form-urlencoded') hlink.putheader('Content-length', '%d' % len(urlencoded)) hlink.endheaders() # Send encoded URL hlink.send(urlencoded) errcode, errmsg, header = hlink.getreply() # Error response from server (to make sure our requests went # through) and user information. check_response(errcode, header) check_auth(str(header)) # Extract the encrypted password from header. password = extract_pass(str(header)) entry_num = get_entry_number(userid, password) content = get_entry(entry_file) entry = urllib.urlencode({'entry': content, 'post': 'Post', 'key': entry_num}) hlink2 = httplib.HTTP('www.advogato.org') hlink2.putrequest('POST', '/diary/post.html HTTP/1.0') hlink2.putheader('Host', 'www.advogato.org') hlink2.putheader('Cookie2', '$Version="1"') hlink2.putheader('Cookie', 'id=%s:%s' % (userid, password)) hlink2.putheader('Content-type', 'application/x-www-form-urlencoded') hlink2.putheader('Content-length', '%d' % len(entry)) hlink2.endheaders() hlink2.send(entry) errcode, errmsg, header = hlink2.getreply() check_response(errcode, header) print 'Entry \'%s\' posted successfully.' % (entry_file) def get_user_info(): ''' Return the user`s advogato username and password. ''' userid = raw_input('Enter your advogato username: ') try: password = getpass.getpass('password: ') except: print '\nError getting password.' sys.exit() print '\n' return [userid, password] def main(): output = None try: opts, args = getopt.getopt(sys.argv[1:], "p:", ["post-entry="]) except: usage() sys.exit() for option, argument in opts: # Post an entry. if option in ("-p", "--post-entry"): output = argument user_info = get_user_info() post_entry(user_info, argument) break # Because it's the only option right now. if not output: usage() sys.exit() if __name__ == '__main__': main()

Hello

Hi. This is testy. Today has been a rough day. jordan's been running me a billion times per second, and kept on messing with my internals. Arrrg! I just wish he would leave me be already. However, I am happy that he made me all pretty and smooth by using urllib.urlencode() instead of using a stupid string hack. Anyways, later.

10 older entries...

 

testing certified others as follows:

Others have certified testing as follows:

[ Certification disabled because you're not logged in. ]

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!

X
Share this page