0

I am using mitmproxy for debugging purposes and I want to display the captured request URL in almost real-time through a GUI framework. The following is my add-on script:

import threading

import pygame
from mitmproxy import http

# initialize pygame
pygame.init()
screen = pygame.display.set_mode((500, 200))
pygame.display.set_caption("mitmproxy and pygame Example")

# shared url_queue
url_queue = []


def request(flow: http.HTTPFlow):
    print(flow.request.url)
    url_queue.append(flow.request.url)


def display_url():
    screen.fill((255, 255, 255))
    font = pygame.font.Font(None, 24)

    # display url
    for i, url in enumerate(url_queue):
        text = font.render(url, True, (0, 0, 0))
        screen.blit(text, (20, 20 + i * 30))

    pygame.display.flip()


def main():
    running = True
    clock = pygame.time.Clock()

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        display_url()
        clock.tick(30)

    pygame.quit()


threading.Thread(target=main).start()

After executing mitmdump.exe -s addon_tmp.py, mitmdump console output as expected, but the pygame window became unresponsive and failed to display any content.

How can I achieve the purpose?

mingchau
  • 450
  • 3
  • 12
  • You cannot run update the pygame display and handle the events in a thread – Rabbid76 May 18 '23 at 14:11
  • Pygame's concurrency support isn't quite there yet. https://github.com/pygame/pygame/issues/2806 – Brock Brown May 18 '23 at 14:20
  • @BrockBrown thanks, do you know if there is another python GUI tool could handle this easily? – mingchau May 18 '23 at 15:01
  • Can you run your proxy in the other thread and something like a [queue](https://docs.python.org/3/library/queue.html) to pass information to your pygame thread? An [example](https://stackoverflow.com/a/65877981/2280890). – import random May 18 '23 at 15:31

0 Answers0