以管理员身份运行 python 文件

我正在尝试创建一个程序,要求任何网站的用户链接阻止它(在主机中)


import requests

print('Time to block some websites')

ask = input('> Give me the link... ') ; link = {} # Ask user

try:

    r = requests.get(ask) # Try the url 

    print('Protocol : ' , r.url [:r.url.find(":")]) # Check protocol of the url

    url = r.url 

    print('your url : ' , url)

except:

    raise ValueError('Give the url with https/http', ask)


url = url.split('/') ; urlist = list(url) # Split url 

link.update({'linko': urlist[2]}) ; link.update({'host': '127.0.0.1 '}) # Add host and url to link

x = link['linko'] ; y = link['host'] # Transform value of dict in string

z = str(y+x) # Assign host and link


f = open('hosts', 'a') # Open document in append mode

f.write(z) ; f.write('\n') # Write z and newline for future use

f.close() # Always close after use


f = open('hosts' , 'r') # Open document in read mode

print(f.read()) # Read document

f.close() # Always close after use

追溯 :


  File "C:\Windows\System32\drivers\etc\block.py", line 17, in <module>

    f = open('hosts', 'a') # Open document in append mode

PermissionError: [Errno 13] Permission denied: 'hosts'

当我尝试使用 runas 管理员执行程序时:


RUNAS ERROR: Unable to run - C:\Windows\System32\drivers\etc\block.py

193: C:\Windows\System32\drivers\etc\block.py is not a valid Win32 application. 

如何让程序有权向主机添加站点?


神不在的星期二
浏览 44回答 0
0回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python