簡體   English   中英

如何在不使用子流程或pexpect模塊的情況下將輸入值發送到終端

[英]How to send input values to terminal without using subprocess or pexpect modules

我正在嘗試為python腳本編寫單元測試。 無論是否需要代理憑據,我的腳本都會接受用戶輸入。 我已經看到了幾個answer-1answer-2answer-3,就像用subprocess和pexpect執行一樣。 但是我的意圖是,在接受用戶輸入后,我必須在腳本中執行其他功能。 因此,執行腳本無濟於事。 我的python腳本如下。 有人可以向我提供建議或實現該建議的方法嗎?

import getpass
class ProxyDetails:
    def __init__(self,option):
        self.option = option
        self.proxy_option = self.get_web_proxy_details()
        self.call_request()
        self.parse_test()

    def get_web_proxy_details(self):
        if self.option == "Default":
            choice = raw_input("Enter credentials : Y/N ").lower()
            if choice.lower() == "y":
                self.proxy_username = getpass.getpass("Enter Username for Proxy : ")
                self.proxy_password = getpass.getpass("Enter Password for Proxy : ")
                self.requireProxyCredentials = "Y"
            elif choice.lower() == "n":
                print("credentials are not present ")
        else:
            print("proxy is none ")

    def call_request(self):
        # this method will do API call
        pass
    def parse_test(self):
        #this method will parse the json
        pass

obj=ProxyDetails(option="Default")

我想在提示Enter credentials : Y/N時發送輸入值Enter credentials : Y/N否。

實際上,這是一個重復的問題,我沒有刪除它,因為它可能對其他人有用。

import sys
import StringIO
f1 = sys.stdin
f = StringIO.StringIO('n') 
sys.stdin = f
obj=ProxyDetails(option="Default")
sys.stdin = f1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM