python通过指定网卡发包_Python 使用指定的网卡发送HTTP请求的实例

本文介绍如何在Python中通过指定网卡发送HTTP请求。通过修改socket.py和利用`source_address`参数,可以实现从不同网卡进行连接。测试表明,通过设置`socket.create_connection`函数的`source_address`参数为对应网卡的IP和端口0,可以在多网卡环境中选择特定网卡发送数据。
摘要由CSDN通过智能技术生成

需求: 一台机器上有多个网卡, 如何访问指定的 URL 时使用指定的网卡发送数据呢?

$ curl --interface eth0 www.baidu.com # curl interface 可以指定网卡

阅读 urllib.py 的源码, 追述到 open_http –> httplib.HTTP –> httplib.HTTP._connection_class = HTTPConnection

HTTPConnection 在创建的时候会指定一个 source_address.

HTTPConnection.connect 时调用 HTTPConnection._create_connection = socket.create_connection

# 先看一下本地网卡信息

$ ifconfig

lo0: flags=8049 mtu 16384

options=3

inet6 ::1 prefixlen 128

inet 127.0.0.1 netmask 0xff000000

inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1

nd6 options=1

en0: flags=8863 mtu 1500

ether c8:e0:eb:17:3a:73

inet6 fe80::cae0:ebff:fe17:3a73%en0 prefixlen 64 scopeid 0x4

inet 192.168.20.2 netmask 0xffffff00 broadcast 192.168.20.255

nd6 options=1

media: autoselect

status: active

en1: flags=8863 mtu 1500

options=4

ether 0c:5b:8f:27:9a:64

inet6 fe80::e5b:8fff:fe27:9a64%en8 prefixlen 64 scopeid 0xa

inet 192.168.8.100 netmask 0xffffff00 broadcast 192.168.8.255

nd6 options=1

media: autoselect (100baseTX )

status: active

可以看到en0和en1, 这两块网卡都可以访问公网. lo0是本地回环.

直接修改 socket.py 做测试.

def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,

source_address=None):

"""If *source_address* is set it must be a tuple of (host, port)

for the socket to bind as a source address before making the connection.

An host of '' or port 0 tells the OS to use the default.

source_address 如果设置, 必须是传递元组 (host, port), 默认是 ("", 0)

"""

host, port = address

err = None

for res in getaddrinfo(host, port, 0, SOCK_STREAM):

af, socktype, proto, canonname, sa = res

sock = None

try:

sock = socket(af, socktype, proto)

# sock.bind(("192.168.20.2", 0)) # en0

# sock.bind(("192.168.8.100", 0)) # en1

# sock.bind(("127.0.0.1", 0)) # lo0

if timeout is

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值