【问题标题】:mimtproxy running python script: "XX Module not imported"mimtproxy 运行 python 脚本:“XX 模块未导入”
【发布时间】:2023-03-12 01:03:01
【问题描述】:

所以我为 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

【问题讨论】:

    标签: python-3.x mitmproxy


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-01
      • 2016-08-01
      • 2011-12-19
      • 2021-12-29
      相关资源
      最近更新 更多