我用python创建了一个登录系统。它工作得很好,但我想在服务器或网络上运行脚本。例如:Steam。 Steam想要用户名和密码登录。所以我想为我的脚本做同样的事情。我怎么能这样做?
我的代码:
import os
import string
import time
version = "1.0 Alfa"
def login():
print ("----------------------------------------")
print (" Login ")
print ("----------------------------------------")
k_name = input("Enter username: ")
if os.path.exists(k_name + ".txt") == False:
print ("Username not found.")
create()
else:
k_pass = input("Enter password: ")
with open(k_name + ".txt", "r") as f:
if k_pass == f.read():
print("Welcome %s!"%k_name)
f.close()
input()
else:
print("Password is wrong!")
create()
def create():
print("You using login system %s" % version)
print( "----------------------------------------")
print("| Lobby |")
print( "----------------------------------------")
starting = input("To create user type R, to login type L").upper()
if starting == "R":
name = input("Enter username: ")
password = input("Enter password: ")
password2 = input("Enter password again: ")
if password == password2:
newfile = open(name + ".txt", "w")
newfile.write(password)
newfile.close()
print("User created. Redirecting you to login.")
time.sleep(2)
login()
elif password != password2:
print("Passwords doesn't match.")
input()
create()
elif starting == "L":
login()
else:
print("\nWrong button\n")
create()
create()
答案 0 :(得分:0)
以下是您可以运行的脚本:python test.py user pass 如果找不到文件,它将保存数据并执行登录
#!/usr/bin/env python
import sys, getopt
import os
import time
version = "1.0 Alfa"
def login(username=None, password=None):
print ("----------------------------------------")
print (" Login ")
print ("----------------------------------------")
if username:
k_name = username
else:
k_name = input("Enter username: ")
if os.path.exists(k_name + ".txt") == False:
print ("Username not found.")
create(username, password, "R")
else:
if password:
k_pass = password
else:
k_pass = input("Enter password: ")
with open(k_name + ".txt", "r") as f:
if k_pass == f.read():
if not username:
print("Welcome %s!"%k_name)
f.close()
input()
else:
print("Password is wrong!")
create()
def create(username=None, password=None, mode="L"):
print("You using login system %s" % version)
print( "----------------------------------------")
print("| Lobby |")
print( "----------------------------------------")
if mode:
starting = mode
else:
starting = input("To create user type R, to login type L").upper()
if starting == "R":
if username:
name = username
else:
name = input("Enter username: ")
if password:
password2 = password
else:
password = input("Enter password: ")
password2 = input("Enter password again: ")
if password == password2:
newfile = open(name + ".txt", "w")
newfile.write(password)
newfile.close()
print("User created. Redirecting you to login.")
time.sleep(2)
login(username, password)
elif password != password2:
print("Passwords doesn't match.")
input()
create()
elif starting == "L":
login(username, password)
else:
print("\nWrong button\n")
create()
def main(argv):
print sys.argv
if len(sys.argv) < 3:
print 'test.py <username> <password>'
sys.exit()
username = sys.argv[1]
password = sys.argv[2]
print 'username is ', username
print 'password is ', password
create(username, password)
if __name__ == "__main__":
main(sys.argv[1:])