Python Requests IP直连

import re
import requests
from urllib3.util import connection

_orig_create_connection = connection.create_connection


def patched_create_connection(address, *args, **kwargs):
    s = str(address[1])
    if len(s) == 18:
        port = int(s[1:6])
        ip = str(int(s[6:9])) + '.' + str(int(s[9:12])) + '.' + str(int(s[12:15])) + '.' + str(int(s[15:18]))
    else:
        port = address[1]
        ip = address[0]
    return _orig_create_connection((ip, port), *args, **kwargs)


connection.create_connection = patched_create_connection


def get(url, ip, params=None, **kwargs):
    str_port = '100080'
    str_ip = ''
    for ipe in ip.split('.'):
        str_ip += ipe.zfill(3)
    mc = re.match(r'^((https?)://([^/:]+))(?::(\d+))?', url, re.I)
    if mc:
        if mc.group(2):
            if mc.group(2).lower() == 'https':
                str_port = '100443'
        if mc.group(3):
            if 'headers' not in kwargs:
                kwargs['headers'] = {}
            if 'host' not in kwargs['headers']:
                kwargs['headers']['host'] = mc.group(3)
        if mc.group(4):
            str_port = '1' + mc.group(4).zfill(5)
        url = url.replace(mc.group(0), mc.group(1) + ':' + str_port + str_ip, 1)
    return requests.get(url, params=params, **kwargs)

  

转载于:https://www.cnblogs.com/j4s0n/p/10737788.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值