如何从python脚本通过浮动ip发送Digitalocean出站流量?

时间:2018-03-27 09:01:33

标签: python proxy python-requests firewall droplet

首先感谢您的帮助,我正试图解决这个问题好几天

我知道可以通过浮动IP(可以分配给Droplet的可公开访问的静态IP地址)路由来自DigitalOcean Droplet的出站流量。

所以,我的Droplet有2个ip(常规和浮动ip) 正如此链接https://www.digitalocean.com/community/questions/send-outbound-traffic-over-floating-ip中所建议的那样,我用

找到了我的Droplet的“锚IP”
ip addr show eth0

我想用python发出请求,使用我的IP作为代理。我可以做这个?例如,有些东西:

import requests

proxies = {
    'http': 'http://XX.XX.XX.XX:YY',
    'https': 'http://XX.XX.XX.XX:YY',
}

# Create the session and set the proxies.
s = requests.Session()
s.proxies = proxies

# Make the HTTP request through the session.
r = s.get('https://www....')

XX.XX.XX.XX是我的主播ip

YY - >我必须使用哪个端口?

我必须在我的防火墙(UFW)中添加一些规则吗?

提前致谢

1 个答案:

答案 0 :(得分:3)

首先,在Dropbox上分配浮动IP。

  1. 安装squid

    apt-get update apt-get install -y squid

  2. 配置squid代理:在Google上搜索如何进行基本配置。

  3. 找到您的Droplet"锚定IP"与

    ip addr show eth0

  4. 这是输出的一个例子:

    `inet "YOUR_IP1"/20 brd 123.456.789.012 scope global eth0
       valid_lft forever preferred_lft forever
    
    inet "YOUR_IP2"/16 brd 12.34.567.890 scope global eth0
       valid_lft forever preferred_lft forever
    

    `

    1. 现在,您的Droplet有2个ip: YOUR_IP1 YOUR_IP2

      将这些行添加到/etc/squid/squid.conf:

      acl myip_1 myip YOUR_IP1 tcp_outgoing_address YOUR_IP1 myip_1

      acl myip_2 myip 10.17.0.5 tcp_outgoing_address 10.17.0.5 myip_2

    2. 在python中编写一个脚本:

      import requests

      proxies = { 'http': 'http://PUBLIC_IP:PORT', 'https': 'http://PUBLIC_IP:PORT', }

      # Create the session and set the proxies. s = requests.Session() s.proxies = proxies

      # Make the HTTP request through the session. r = s.get('https://www....')