Traceback (most recent call last): File "D:\JetBrains\PyCharm\plugins\python\helpers\pydev\pydevconsole.py", line 364, in runcode coro = func() ^^^^^^ File "<input>", line 1, in <module> File "D:\JetBrains\PyCharm\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 198, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\JetBrains\PyCharm\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "D:\Projects\pythonProject\停车系统\temp.py", line 45, in <module> close_proxy() File "D:\Projects\pythonProject\停车系统\temp.py", line 39, in close_proxy mitmdump_process.stop() ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'Popen' object has no attribute 'stop'
时间: 2023-07-21 07:35:11 浏览: 169
抱歉,我的回答有误。Popen对象没有stop()方法,您应该使用terminate()方法来停止进程。具体代码示例如下:
```
from subprocess import Popen
class AddHeader:
def request(self, flow: http.HTTPFlow) -> None:
self.num += 1
if "https://cloud.keytop.cn/service/front/userCenter/lpn/list?_=" in flow.request.pretty_url:
for name, value in flow.request.headers.items():
if "accesstoken:" == name:
accesstoken = value
addons = [
AddHeader()
]
def set_proxy():
"""设置系统代理"""
global mitmdump_process
pxy.enable = True
pxy.server = '127.0.0.1:8081'
pxy.registry_write()
mitmdump_process = Popen(['mitmdump', '-p', '8081', '-s', __file__])
def close_proxy():
"""关闭系统代理"""
pxy.enable = False
pxy.registry_write()
mitmdump_process.terminate()
if __name__ == "__main__":
pxy = ProxySetting()
set_proxy()
close_proxy()
```
在这个示例中,我们使用了subprocess模块中的Popen()函数来创建mitmdump进程对象,并将其赋值给全局变量mitmdump_process。在close_proxy()中,我们只需要调用mitmdump进程对象的terminate()方法即可停止mitmdump进程。
阅读全文