【问题标题】:How to send Digitalocean outbound traffic over floating ip from a python script?如何从 python 脚本通过浮动 ip 发送 Digitalocean 出站流量?
【发布时间】:2018-09-05 14:34:39
【问题描述】:

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

我知道可以通过其浮动 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

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

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) 添加一些规则?

提前致谢

【问题讨论】:

  • 请求的代理功能用于HTTP代理服务器。如果您希望流量通过机器上的特定接口进行路由,您应该使用操作系统中的路由功能(例如 Linux 中的route)。

标签: python proxy python-requests firewall droplet


【解决方案1】:

首先,在你的 Droplet 上分配一个浮动 IP。

  1. 安装鱿鱼

    apt-get update apt-get install -y squid

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

  3. 使用

    找到你的Droplet的“锚IP”

    ip addr show eth0

这是一个输出示例:

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_IP1YOUR_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

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....')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    相关资源
    最近更新 更多