# How to set an external proxy in script?

**URL:** <https://discourse.mitmproxy.org/t/how-to-set-an-external-proxy-in-script/1594>\
**Category:** help\
**Created:** [June 27, 2019, 10:19am UTC](https://discourse.mitmproxy.org/t/how-to-set-an-external-proxy-in-script/1594 "2019-06-27T10:19:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheGrado](https://avatars.discourse-cdn.com/v4/letter/t/f08c70/32.png) [@TheGrado](https://discourse.mitmproxy.org/u/TheGrado)\
**Post date:** [June 27, 2019, 10:19am UTC](https://discourse.mitmproxy.org/t/how-to-set-an-external-proxy-in-script/1594/1 "2019-06-27T10:19:40Z")

</div>

I’m trying to make a python3.7 script that switches the upstream proxy to an external one for every request that goes trouh the mitm proxy, the structure should look like this:  
CLIENT \> MITM \> EXTERNAL USER:AUTH PROXY \> DESTINATION SERVER

This is what i have accomplished so far:

```
class MITM:

    def request(self, flow):
        if flow.request.method == "CONNECT":
            return

        if flow.live:
            proxy_data = self.switchProxy()
            ctx.log.info('Setting upstream proxy to '+proxy_data.hostname+' and port '+str(proxy_data.port))
            flow.live.change_upstream_proxy_server((proxy_data.hostname, proxy_data.port))
            flow.request.headers["Proxy-Authorization"] = b"Basic"+b" "+base64.b64encode(strutils.always_bytes(proxy_data.username+':'+proxy_data.password))

    def switchProxy(self):
        proxy_data = urlparse('http://user:pass@ip:port')
        return proxy_data

addons = [
    MITM()
]

```

But this does not seems to work, when i run

```
mitmdump --upstream http://localhost:8080/ -s mitmserv.py

```

and make a request trough the mitm proxy to

> [https://api.ipify.org/?format=json](https://api.ipify.org/?format=json)

the returning IP address is my home IP, instead of the external proxy.
