LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-20-2009, 02:33 AM   #1
zux
Member
 
Registered: Jul 2006
Location: latvia
Distribution: slackware
Posts: 127

Rep: Reputation: 16
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

Last edited by zux; 01-20-2009 at 02:57 AM.
 
Old 01-20-2009, 05:55 AM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 40
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().
 
Old 01-20-2009, 07:49 AM   #3
zux
Member
 
Registered: Jul 2006
Location: latvia
Distribution: slackware
Posts: 127

Original Poster
Rep: Reputation: 16
ok so far so good (i'm completly new to python) how do i return that a function is true?

Last edited by zux; 01-20-2009 at 08:09 AM.
 
Old 01-20-2009, 08:56 AM   #4
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Code:
return True
 
Old 01-20-2009, 09:51 AM   #5
zux
Member
 
Registered: Jul 2006
Location: latvia
Distribution: slackware
Posts: 127

Original Poster
Rep: Reputation: 16
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
basic python entry tkinter widget how-to donnied Programming 2 03-30-2008 10:36 AM
Gateway Popup Athentication abakali Linux - Networking 1 07-09-2006 04:35 PM
LXer: Move to python 2.4 / Changing the packaging style for python packages LXer Syndicated Linux News 0 06-13-2006 07:54 PM
Error in basic button response program in Python 2.4 with the Tkinter module jojotx0 Programming 1 05-23-2006 07:43 PM
types of athentication in BSD family. m_pahlevanzadeh Programming 0 07-26-2004 06:34 PM


All times are GMT -5. The time now is 10:23 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration