Python 表示它是 运行 提升的访问权限,但它仍然返回错误?
Python says it is running with elevated access but it is still returning an error?
我最近试图创建一个脚本来跟踪传出的 TCP 连接,但我 运行 在尝试 运行 TCPView.exe 时遇到错误。下面的脚本应该 g运行t 提升访问权限,但它仍然 returns 这个: OSError: [WinError 740]
请求的操作需要提升。我认为这是因为 python
的 g运行ting 权限具有提升的访问权限,而不是 TCPView
。仍然不确定如何解决这个问题。
from subprocess import Popen, PIPE
import sys
import os
import subprocess
import sys
import win32com.shell.shell as shell
def get_tcp_conns():
process = Popen([r"C:\Users\PC\Downloads\TCPView\Tcpview.exe"], stdout=PIPE)
for line in iter(process.stdout.readline, b''):
print(line) # Do whatever here
ASADMIN = 'asadmin'
if sys.argv[-1] != ASADMIN:
script = os.path.abspath(sys.argv[0])
params = ' '.join([script] + sys.argv[1:] + [ASADMIN])
shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params)
get_tcp_conns()
with open("somefilename.txt", "w") as out:
print(out, "i am root")
get_tcp_conns()
而不是这个:
process = Popen([r"C:\Users\PC\Downloads\TCPView\Tcpview.exe"], stdout=PIPE)
试试这个:
process = Popen([r"C:\Users\PC\Downloads\TCPView\Tcpview.exe"], stdout=PIPE, shell=True)
即设置shell
参数为True
。
我最近试图创建一个脚本来跟踪传出的 TCP 连接,但我 运行 在尝试 运行 TCPView.exe 时遇到错误。下面的脚本应该 g运行t 提升访问权限,但它仍然 returns 这个: OSError: [WinError 740]
请求的操作需要提升。我认为这是因为 python
的 g运行ting 权限具有提升的访问权限,而不是 TCPView
。仍然不确定如何解决这个问题。
from subprocess import Popen, PIPE
import sys
import os
import subprocess
import sys
import win32com.shell.shell as shell
def get_tcp_conns():
process = Popen([r"C:\Users\PC\Downloads\TCPView\Tcpview.exe"], stdout=PIPE)
for line in iter(process.stdout.readline, b''):
print(line) # Do whatever here
ASADMIN = 'asadmin'
if sys.argv[-1] != ASADMIN:
script = os.path.abspath(sys.argv[0])
params = ' '.join([script] + sys.argv[1:] + [ASADMIN])
shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params)
get_tcp_conns()
with open("somefilename.txt", "w") as out:
print(out, "i am root")
get_tcp_conns()
而不是这个:
process = Popen([r"C:\Users\PC\Downloads\TCPView\Tcpview.exe"], stdout=PIPE)
试试这个:
process = Popen([r"C:\Users\PC\Downloads\TCPView\Tcpview.exe"], stdout=PIPE, shell=True)
即设置shell
参数为True
。