# Start and stop mitmproxy from Python script

**URL:** <https://discourse.mitmproxy.org/t/start-and-stop-mitmproxy-from-python-script/677>\
**Category:** help\
**Created:** [October 28, 2017, 1:59pm UTC](https://discourse.mitmproxy.org/t/start-and-stop-mitmproxy-from-python-script/677 "2017-10-28T13:59:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![U039b](https://avatars.discourse-cdn.com/v4/letter/u/59ef9b/32.png) [@U039b](https://discourse.mitmproxy.org/u/U039b)\
**Post date:** [October 28, 2017, 1:59pm UTC](https://discourse.mitmproxy.org/t/start-and-stop-mitmproxy-from-python-script/677/1 "2017-10-28T13:59:11Z")

</div>

Hi all!  
I wonder how to start and stop MITMProxy from a Python3 program. I need to programmatically start MITMProxy and exit it in a clean way in order to be sure that the output file is correctly flushed on disk.

Do you have any recommendations, hints, snippets, whatever about that?

Cheers!!

---

<div class="post-metadata">

**Author:** ![mhils](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/mhils/32/7_2.png) [@mhils](https://discourse.mitmproxy.org/u/mhils)\
**Post date:** [October 30, 2017, 4:47pm UTC](https://discourse.mitmproxy.org/t/start-and-stop-mitmproxy-from-python-script/677/2 "2017-10-30T16:47:00Z")

</div>

What exactly are you trying to accomplish? You can either run mitmdump as a regular process and send it a Ctrl+C when you are finished (importing `mitmdump` from `mitmproxy.tools.main` would work as well), or you instantiate your own DumpMaster and thenn call `master.shutdown()` when you are finished.

---

<div class="post-metadata">

**Author:** ![Gigiarum](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/gigiarum/32/278_2.png) [@Gigiarum](https://discourse.mitmproxy.org/u/Gigiarum)\
**Post date:** [September 18, 2018, 2:15pm UTC](https://discourse.mitmproxy.org/t/start-and-stop-mitmproxy-from-python-script/677/3 "2018-09-18T14:15:22Z")

</div>

I solved this problem in my own script calling

> from mitmproxy import ctx  
> ctx.master.shutdown()

mitmproxy v4.0.1

---

<div class="post-metadata">

**Author:** ![dianadevasia](https://avatars.discourse-cdn.com/v4/letter/d/d26b3c/32.png) [@dianadevasia](https://discourse.mitmproxy.org/u/dianadevasia)\
**Post date:** [April 9, 2019, 12:09am UTC](https://discourse.mitmproxy.org/t/start-and-stop-mitmproxy-from-python-script/677/4 "2019-04-09T00:09:38Z")

</div>

@Gigiarum and @mhils:  
Can you please elaborate how to start and stop mitm programatically?

---

<div class="post-metadata">

**Author:** ![Gigiarum](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mitmproxy.org/gigiarum/32/278_2.png) [@Gigiarum](https://discourse.mitmproxy.org/u/Gigiarum)\
**Post date:** [April 9, 2019, 7:58am UTC](https://discourse.mitmproxy.org/t/start-and-stop-mitmproxy-from-python-script/677/5 "2019-04-09T07:58:05Z")

</div>

I create a system service that call my script launching mitmdump.
