Linux 编程

Linux 编程

我想用 c#、c++ 或任何编程语言甚至像 bash 这样的脚本语言创建一个应用程序。

应用程序将要求输入答案,例如 1+1?密码为 2,如果正确则将登录。我不想回答如何编写应用程序,只想回答如何在 Linux 中实现该操作。

我如何在 ubuntu 中实现该功能?(10.04 或 12.04)

提前谢谢你,最好的问候

答案1

代码没有提供使用 256 位密钥通过某些网络基础设施进行身份验证的方法。它只是提供了一个工作计划。OP 必须根据计划完成他的作业……

下面是一段简单的 Python 代码,用于执行一个小型益智游戏,代表了大部分所需逻辑。请参阅代码中的注释,了解有关 Python 的在线资源以及有关每行代码的一些提示。

更新

按照 OP 的要求添加了启动 X 服务器的代码。代码使用了 subprocess 模块。我还没有测试 start_x 函数,但它应该接近所要求的功能。

#!/usr/bin/env python
"""A mini puzzle game using Python 2.7.5 programming language
and the random module from the standard library.
See:
Website http://www.python.org
Python 2.7.5 documentation http://docs.python.org/2/
Online Python books
http://www.diveintopython.net/
http://wiki.python.org/moin/PythonBooks
"""
import random
#import the subprocess module
import subprocess


def do_ask_random_sum_result():
    """Asks for the result of the addition of two random integers"""

    # get the two random integers from
    # a set of integers {0, 1, 2, 3, 4, 5}
    first_random = random.randint(0,5)
    second_random = random.randint(0,5)

    # perform addition and store the
    # result to random_sum
    random_sum = first_random + second_random
    # print the two random numbers to output
    # asking the user for the result of their
    # addition
    result = input("What is the sum of {0} + {1}? ".format(
        first_random, second_random))
    # check if the user input matches
    # the result
    if random_sum == result:
        # instead of printing something
        # perform the log in to whatever you
        # want the user to log in
        print("That is correct, the result is: {0}".format(random_sum))
        print("Performing log in procedures...")
    # Cannot perform the log in since
    # the user provided a wrong answer...
    else:
        print ("Wrong answer!")

def do_login():
    """Performs the login"""

    # perform various login actions
    # 
    # and finally try to start X server
    try:
        # call the start_x function
        start_x()
    except Exception as e:
        print ("Could not start X server")
        print ("Error details:\n{err}".format(err=e.message))

def start_x():
    """Starts the x server using the subprocess module
        NOT TESTED but it must be something close to this
    """

    #declare proc as new process to launch
    proc = subprocess.Popen("startx", shell=True, stdout=subprocess.PIPE)
    #assign the output of proc to output variable this is optional
    #for just to check what x has to say
    output = subprocess.stdout.read()
    #print the results
    print (output)

def main():
    """Application's main entry"""

    do_ask_random_sum_result()

if __name__ == '__main__':
    main()

相关内容