LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   basic athentication with python (http://www.linuxquestions.org/questions/programming-9/basic-athentication-with-python-698542/)

zux 01-20-2009 02:33 AM

basic athentication with python
 
so all i want is a smaal program wich is called in command line:
Code:

auth.py
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):
Code:

#!/usr/bin/python

import sys
import socket

def matchpasswd(login, password):
        if login == "user" and password == "passwd":
                x = 1
        else:
                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):
                sys.stdout.write('OK\n')
        else:
                sys.stdout.write('ERR\n')

        sys.stdout.flush()

but when running it gives this:
Code:

root@monstrs:/etc/squid# ./custom_auth.py

Traceback (most recent call last):
  File "./custom_auth.py", line 16, in <module>
    line = sys.stdin.strip()
AttributeError: 'file' object has no attribute 'strip'
root@monstrs:/etc/squid#

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

taylor_venable 01-20-2009 05: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 07: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 08:56 AM

Code:

return True
:)

zux 01-20-2009 09:51 AM

hmm that part now looks like this:
Code:

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

but I get an error:
Code:

File "./custom_auth.py", line 8
    return True
        ^

probably a syntax problem?


All times are GMT -5. The time now is 06:51 AM.