mitmproxy 运行 python 脚本:"XX Module not imported"
mimtproxy running python script: "XX Module not imported"
所以我 运行 正在为 windows 设置 mitmproxy 并且我正在尝试 运行 一个将响应和请求保存到 postgresql 中的脚本,为此我使用 sqlalchemy
但是由于某些原因我无法让它与 mimtproxy 一起工作,当 运行ning 看起来像是在使用另一个 python 解释器并且我的代码不起作用时。 mitmproxy 使用的解释器与您安装的解释器不同吗?
命令 运行ning 来自 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 mitmproxy 自己安装中没有的包,你需要通过pip 或pipx 安装mitmproxy。正常的二进制文件包括它们自己的 Python 环境。
所以我 运行 正在为 windows 设置 mitmproxy 并且我正在尝试 运行 一个将响应和请求保存到 postgresql 中的脚本,为此我使用 sqlalchemy
但是由于某些原因我无法让它与 mimtproxy 一起工作,当 运行ning 看起来像是在使用另一个 python 解释器并且我的代码不起作用时。 mitmproxy 使用的解释器与您安装的解释器不同吗?
命令 运行ning 来自 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 mitmproxy 自己安装中没有的包,你需要通过pip 或pipx 安装mitmproxy。正常的二进制文件包括它们自己的 Python 环境。