-   Programming (
-   -   basic athentication with python (

zux 01-20-2009 03:33 AM

basic athentication with python
so all i want is a smaal program wich is called in command line:
and then it waits for username and password
and it should return OK or ERR

it should only check one username and password and it would be hardcoded in it :)

so I have somthing like this (parts of it stolen from somewhere):


import sys
import socket

def matchpasswd(login, password):
        if login == "user" and password == "passwd":
                x = 1
                x = 0

while True:
        line = sys.stdin.readline()

        line = sys.stdin.strip()

        username = line[:line.find(' ')]
        password = line[line.fine(' ')+1:]

        if matchpasswd(username, password):


but when running it gives this:

root@monstrs:/etc/squid# ./

Traceback (most recent call last):
  File "./", line 16, in <module>
    line = sys.stdin.strip()
AttributeError: 'file' object has no attribute 'strip'

i don't even supply a username and password, just pressed enter, the same if isuply a user

taylor_venable 01-20-2009 06:55 AM

Just as it says, you have sys.stdin.strip() -- where sys.stdin is a file object and you're trying to call a non-existent method strip() on it. I think you meant line.strip() or attach the strip() to the readline() on the prior source line.

Another problem that will come up is you have line.fine() rather than line.find(). And you don't effectively handle the case where there is no space. matchpasswd() should be returning True or False, not setting x to zero or one. You would probably be better served by print or print() [Python 3] than sys.stdout.write() and sys.stdout.flush().

zux 01-20-2009 08:49 AM

ok so far so good (i'm completly new to python) how do i return that a function is true?

indienick 01-20-2009 09:56 AM


return True

zux 01-20-2009 10:51 AM

hmm that part now looks like this:

def matchpasswd(login, password):
        if login == "user" and password == "pass":
        return True

but I get an error:

File "./", line 8
    return True

probably a syntax problem?

All times are GMT -5. The time now is 12:20 PM.