Last active
July 10, 2023 06:33
-
-
Save fufuok/79d54036b0d25032da955f2991ed142b to your computer and use it in GitHub Desktop.
python3 requests bind source IP request. 绑定接口IP(源IP)请求URL.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding:utf-8 -*- | |
""" | |
demo-requests-bind-src-ip.py | |
~~~~~~~~ | |
:author: Fufu, 2022/12/28 | |
""" | |
import socket | |
import sys | |
import requests | |
class BindSrcIP: | |
def __init__(self, ip: str): | |
self.src_ip = ip | |
self.socket = socket.socket | |
def __enter__(self): | |
def src_socket(*args, **kwargs): | |
s = self.socket(*args, **kwargs) | |
s.bind((self.src_ip, 0)) | |
return s | |
socket.socket = src_socket | |
def __exit__(self, type, value, trace): | |
socket.socket = self.socket | |
if __name__ == '__main__': | |
src_ip = sys.argv[1] if len(sys.argv) > 1 else '0.0.0.0' | |
with BindSrcIP(src_ip): | |
r = requests.get('https://ipinfo.io') | |
print(r.text) |
Author
fufuok
commented
Jul 10, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment