[Zope] Python Question
Chris Withers
chris at simplistix.co.uk
Fri May 21 03:57:22 EDT 2004
Laura McCord wrote:
> I want to modify my python code to append new required users to my
> .htaccess file
Why? USe Zope's security model, not Apache's. Zope' is much more flexible...
> 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"):
you should be using os.path.exists here.
> dirpath.append('Require user ' + username + '\n')
huh? um, do you mean:
f = open(dirpath,'a')
f.write('Require user ' + username + '\n')
f.close()
?
> 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
I dunno how /usr/bin/htpasswd works, but an os. call like that may want to be
wrapped in a lock unless it's re-entrant...
Chris
--
Simplistix - Content Management, Zope & Python Consulting
- http://www.simplistix.co.uk
More information about the Zope
mailing list