Giter Site home page Giter Site logo

alaska's Introduction

  • 👋 Hi, I’m @JackyXiong
  • 👀 I’m interested in ...
  • 🌱 I’m currently learning ...
  • 💞️ I’m looking to collaborate on ...
  • 📫 How to reach me ...

alaska's People

Watchers

 avatar

alaska's Issues

Html & Dom

const input = document.createElement('input');
document.body.appendChild(input);
input.setAttribute('value', 'test');
input.setAttribute('hidden', true); 
document.execCommand('copy')

用js复制时,执行了input.setAttribute('hidden', true);会无法复制,且不报错。

requests 自定义请求源ip地址

from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager

import requests
import gevent


class CustomSourceAddrAdpater(HTTPAdapter):

    def __init__(self, *args, **kwargs):
        source_address = kwargs.pop('source_address')
        assert isinstance(source_address, tuple) and len(source_address) == 2
        self.source_address = source_address
        super(CustomSourceAddrAdpater, self).__init__(*args, **kwargs)

    def init_poolmanager(self, connections, maxsize, block=False):
        self.poolmanager = PoolManager(num_pools=connections,
                                       maxsize=maxsize,
                                       block=block,
                                       source_address=self.source_address)


s = requests.Session()
a = CustomSourceAddrAdpater(source_address=(random.choice(ips), 0))
s.mount('http://', a)
resp = s.get(url)

原理就是socket连接时bind ip。urllib2也可以实现,但无法使用keep-alive属性。requests 可使用长连接的。
以下urllib2实现

class BindableHTTPConnection(httplib.HTTPConnection):
    def connect(self):
        """Connect to the host and port specified in __init__."""
        self.sock = socket.socket()
        self.sock.bind((self.source_ip, 0))
        if isinstance(self.timeout, float):
            self.sock.settimeout(self.timeout)
        self.sock.connect((self.host, self.port))


def BindableHTTPConnectionFactory(source_ip):
    def _get(host, port=None, strict=None, timeout=0):
        bhc = BindableHTTPConnection(host, port=port, strict=strict, timeout=timeout)
        bhc.source_ip = source_ip
        return bhc
    return _get


class BindableHTTPHandler(urllib2.HTTPHandler):
    def http_open(self, req):
        return self.do_open(BindableHTTPConnectionFactory(random.choice(ips)), req)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.