2017-01-14 4 views
2

たとえば、私は私のpythonスクリプトでシナプスを呼びたい場合、これは私が試みたものですが、Popenでエラーが発生しています。 gksuの代わりにsudoを使ってこの作業をするにはどうしたらいいですか?このメソッドを使用して、より大きなプログラム内でスクリプトを実行したいと考えています。以下は私はpythonでアプリケーションを開くためにsudoを実行しようとしています

process = subprocess.Popen("sudo synaptic", 'w', stdout=subprocess.PIPE, bufsize=1).write(password) 
TypeError: __init__() got multiple values for keyword argument 'bufsize' 

私はあなたがsudoコマンドにパスワードを送信するためにcommunicateを使うべきだと思う私は

from PyQt4 import QtGui, QtCore 
import os 
import sys 
import subprocess 
# from mainwindow import Ui_MainWindow 

class PasswordDialog(QtGui.QDialog): 
    def __init__(self, parent=None): 
     super(PasswordDialog, self).__init__(parent) 
     PasswordDialog.resize(self, 375, 130) 
     PasswordDialog.setWindowTitle(self, "Enter Password") 
     self.buttonOk = QtGui.QPushButton(self) 
     self.buttonOk.setText("OK") 
     self.buttonCancel = QtGui.QPushButton(self) 
     self.buttonCancel.setText("Cancel") 
     self.textEdit = QtGui.QLineEdit(self) 
     self.textEdit.setFocus() 

     self.label = QtGui.QLabel(self) 
     font = QtGui.QFont() 
     font.setBold(True) 
     font.setWeight(75) 
     self.label.setFont(font) 
     self.label.setText("Enter your password to perform administrative Tasks") 
     self.label.setWordWrap(True) 
     self.label_2 = QtGui.QLabel(self) 
     self.label_2.setText("Password") 
     self.verticalLayout = QtGui.QVBoxLayout(self) 
     self.verticalLayout.addWidget(self.label) 
     self.horizontalLayout = QtGui.QHBoxLayout(self) 
     self.horizontalLayout.addWidget(self.label_2) 
     self.horizontalLayout.addWidget(self.textEdit) 
     self.verticalLayout.addLayout(self.horizontalLayout) 
     self.horizontalLayout2 = QtGui.QHBoxLayout(self) 
     self.horizontalLayout2.setAlignment(QtCore.Qt.AlignRight) 
     self.horizontalLayout2.addWidget(self.buttonCancel) 
     self.horizontalLayout2.addWidget(self.buttonOk) 
     self.verticalLayout.addLayout(self.horizontalLayout2) 
     self.buttonOk.clicked.connect(self.handleLogin) 
     self.buttonCancel.clicked.connect(self.close) 

    def handleLogin(self): 
     password = self.textEdit.text() 
     process = subprocess.Popen("sudo synaptic", 'w').write(password) 
     #out = process.stdout.read(1) 
     try: 
      subprocess.check_call(process) 
     except subprocess.CalledProcessError as error: 
      print "error code", error.returncode, error.output 



if __name__ == '__main__': 
    app = QtGui.QApplication(sys.argv) 
    login = PasswordDialog() 

    if login.exec_() == QtGui.QDialog.Accepted: 
     window = Window() 
     window.show() 
     sys.exit(app.exec_()) 
+0

https://docs.python.org/3/library/subprocess.html#popen-constructor

かも簡単...ちょうどsubprocessモジュールが提供するcall機能を使用します'sudo'、 'synaptic'] ' – davedwards

+0

' Popen'のドキュメンテーションをチェックする - おそらく他の引数が 'bufsize'に割り当てられる – furas

+0

[Popen](https://docs.python.org/3.5/library/subprocess.html#)を参照してください。 subprocess.Popen):第2引数 'w'が' bufsize'に割り当てられ、 'bufsize'が2つあります。 – furas

答えて

1

で働いているものです。あなたはPopenに間違った引数を渡す... processの値は、バイト文字列でなければなりません

+0

間違ったパスワードが入力された場合、出力をキャッチする方法を知っているかもしれませんか? – answerSeeker

+0

@Tata​​kaiWasumi新しい質問 –

+0

私はいくつかの問題を修正した 'shell = True'を追加しましたが、サブプロセスを使ってエラーが発生しました.Popen([" sudo "、" synaptic "]、stdout = subprocess.PIPE、bufsize = 1、shell = True)をプロセスとして使用しています: AttributeError:__exit__' – answerSeeker

0

import subprocess 

with subprocess.Popen(["sudo", "synaptic"], stdout=subprocess.PIPE, bufsize=1) as process: 
    process.communicate(password) 
    process.wait() 

はこれを試してみてください。ドキュメントはここにある: `subprocess.Popen`は[`、リストで試してみてくださいためにあなたの引数

subprocess.call(['sudo', 'synaptic']) 
+0

しかし、私はself.textEdit.text()からsudoにパスワードを送ることができませんか? – answerSeeker

+0

@たたかわわすみはあなたが尋ねた質問ではありません。あなたは 'sudo synaptic'を呼び出す方法について尋ねました。私は答えを提供した。それはここではどのように動作するのか、まあまあです。スポーンされたサブプロセスと対話することに関するフォローアップの質問がある場合は、新しい質問をしてください。 –

+0

@CoreyGoldberg。 OPに公平であるために、質問のコードには、プロセスにパスワードを書き込もうとする試みが含まれています(ただし、構文はすべて間違っていますが、popenのドキュメントを読んでいないためです)。 – ekhumoro

関連する問題