mimtproxy 运行 python 脚本:“XX 模块未导入”

时间:2021-07-08 17:40:24

标签: python-3.x mitmproxy

所以我在 Windows 上运行 mitmproxy 并且我试图运行一个脚本,将响应和请求保存到 postgresql 中,为此我使用 sqlalchemy

但是由于某种原因我无法让它与 mimtproxy 一起工作,当运行时似乎它使用另一个 python 解释器并且我的代码不起作用。 mitmproxy 使用的解释器是否与您安装的解释器不同?

从 mimtmproxy/bin 文件夹运行的命令:

mitmdump.exe -s C:\users\etc\{FULL_PATH}\mitmproxy.py

我得到了

"No module instaled named SQLAlchemy"

我已经尝试通过 pip 和 pip3 安装该模块告诉我我缺少(sqlalchemy)但它已经安装

enter image description here

mimtproxy.py

from mitmproxy import http
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session

from entities.models.Request import RequestModel
from entities.models.Response import ResponseModel
from entities.models.Session import SessionModel

server = 'www.XXX.com'
world = 'XXX'
user = 'XXX'
version = None

engine = create_engine('postgresql://XXX:XXX@localhost:5432/XXX')
Base = declarative_base()


def createSession():

    with Session(engine) as session:
        http_session = SessionModel(server=server,world=world,version=version,user=user)
        session.add(http_session)
        # We add created object to our DB
        session.flush()
        # At this point, the object  has been pushed to the DB,
        # and has been automatically assigned a unique primary key id
        session.refresh(http_session)
        # refresh updates given object in the session with its state in the DB
        # (and can also only refresh certain attributes - search for documentation)
        return  http_session



session_object = createSession()
with Session(engine) as session:
    session.add(session_object)
    session.commit()


def request(flow: http.HTTPFlow) -> None:

    if flow.request.headers['x-ig-client-version'] and session_object.version == None:
        session_object.version = flow.request.headers['x-ig-client-version']
        with Session(engine) as session:
            session.commit()

    request_url = flow.request.url
    request_cookies = None
    if flow.request.cookies:
        request_cookies = flow.request.cookies

    Request = RequestModel(method=flow.request.method,url=request_url)
    Request.headers = flow.request.headers
    Request.cookies = request_cookies
    Request.body = flow.request.content
    Request.timestamp_start = flow.request.timestamp_start
    Request.timestamp_end = flow.request.timestamp_end
    Request.size = len(flow.request.content)

    Response = ResponseModel(headers=flow.response.headers,
                             status_code=flow.response.status_code,body=flow.response.content)
    Response.cookies = None
    if flow.response.cookies:
        Response.cookies = flow.response.cookies

    Request.response = Response
    session_object.requests.append([Request])
    with Session(engine) as session:
        session.commit()

所有 sqlalchemy 模型都在这里: AttributeError: 'set' object has no attribute '_sa_instance_state' - SQLAlchemy

1 个答案:

答案 0 :(得分:0)

如果要使用 mitmproxy 自己安装中没有包含的 Python 包,需要通过 pip 或 pipx 安装 mitmproxy。正常的二进制文件包括它们自己的 Python 环境。

来源: https://docs.mitmproxy.org/stable/overview-installation/#installation-from-the-python-package-index-pypi