[Zope] Python Question

Laura McCord Laura.McCord at doucet-austin.com
Thu May 20 11:59:19 EDT 2004


I want to modify my python code to append new required users to my
.htaccess file

So my logic is:

if the file already exists:
     append the new user to the file 
     and add a password for the user
 else:
  open a new file 
  write the contents of the .htaccess file
  close file
return user

-- This is what my code looks like --
def createUser(username, basedir):
 import os
 dirpath = "/var/www/html/da/" + basedir + "/.htaccess"
 dirpwd  = "/var/www/html/da/" + basedir + "/.htpasswd"
 if os.path.isfile("dirpath"):
   dirpath.append('Require user ' + username + '\n')
   createpwd(username, basedir)
 else:
  filename=open(dirpath, 'w')
  filename.write('AuthType Basic\n')
  filename.write('AuthName "Password Required"\n')
  filename.write('AuthUserFile ' + dirpwd + '\n')
  filename.write('Require user ' + username + '\n')
  filename.close()
 return username

def createpwd(username,basedir):
 import os
 import string
 length = 8
 pathname = "/var/www/html/da/" + basedir + "/.htpasswd"
 ## passwdgen calls a password generator
 pwdName= passwdgen(length) 

 if os.path.isfile(pathname):
  os.system("usr/bin/htpasswd -b " + pathname + " %s %s" %(username,
pwdName))
 else:
  os.system("/usr/bin/htpasswd -bc " + pathname + " %s %s" %(username,
pwdName))
 return pwdName

Thanks,
 Laura



More information about the Zope mailing list