V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
claysec
V2EX  ›  Python

MitmProxy Python API 脚本无法导入其他模块

  •  
  •   claysec ·
    boku7 · 2019-04-21 12:13:56 +08:00 · 2232 次点击
    这是一个创建于 1825 天前的主题,其中的信息可能已经有所发展或是发生改变。

    起因

    我想按照 MitmProxy API 写个加密的中转转发脚本。却无奈发现无法载入其他包。 Imgur

    环境

    mitmproxy version : 4.0.4
    system: windows10 
    

    代码

    import base64
    import mitmproxy.http
    import pyDes
    import random
    
    
    # 加密
    def encrypt_str(key,data):
        # 加密方法
        method = pyDes.des(key, pyDes.ECB,pad=None, padmode=pyDes.PAD_PKCS5)
        # 执行加密码
        k = method.encrypt(data)
        # 转 base64 编码并返回
        return base64.b64encode(k)
    
    
    # 解密
    def decrypt_str(key,data):
        method = pyDes.des(key, pyDes.ECB,pad=None, padmode=pyDes.PAD_PKCS5)
        # 对 base64 编码解码
        k = base64.b64decode(data)
        # 再执行 Des 解密并返回
        return method.decrypt(k)
    
    class Counter:
        def __init__(self):
            pass
    
        def request(self, flow: mitmproxy.http.HTTPFlow):
            key = str(random.randint(10000000,99999999))
            flow.request.headers['key'] = key
            #print(key)
            #print(encrypt_str(key.encode(encoding="utf-8"),flow.request.content))
    
        def response(self,flow: mitmproxy.http.HTTPFlow):
            key = flow.response.headers['key']
            # print(key)
            # print(decrypt_str(key.encode(encoding="utf-8"),flow.response.content))
    
    addons = [
        Counter()
    ]
    
    
    

    备注

    • 确定该模块已经正确安装在 python3 中。
    • 一些系统自带的包却没有问题。列如 base64,json 等
    第 1 条附言  ·  2019-04-23 17:12:35 +08:00

    问题原因已经找到。是由于windows版本的是只能通过pip去安装。若是通过官网下载的exe进行安装会提示无法找到包 以下是解决方法:

    • pip(3) install mitmproxy 然后将python目录下的script目录加进系统变量进行调用。
    • 在文件中import sys。在sys模块下面加一句代码
    sys.path.apend('python目录的site-packages目录')
    
    

    这两个方法即可解决无法载入包的错误

    1 条回复    2019-04-23 17:14:09 +08:00
    claysec
        1
    claysec  
    OP
       2019-04-23 17:14:09 +08:00
    纠正一下是 append :) 2. 若是通过官网下载的 exe 进行安装请使用第二种方法
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5477 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 06:35 · PVG 14:35 · LAX 23:35 · JFK 02:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.