Created
February 25, 2022 16:49
-
-
Save viz-prakash/8c05d56b6f231e391fa610cd0af0451b to your computer and use it in GitHub Desktop.
Python script to make HTTP connection from fixed port
This file contains 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
from requests.adapters import HTTPAdapter | |
from requests.packages.urllib3.poolmanager import PoolManager | |
import requests | |
class SourcePortAdapter(HTTPAdapter): | |
""""Transport adapter" that allows us to set the source port.""" | |
def __init__(self, port, *args, **kwargs): | |
self._source_port = port | |
super(SourcePortAdapter, 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_port)) | |
s = requests.session() | |
s.mount('http://127.0.0.1:8080', SourcePortAdapter(54321)) | |
s.get('http://127.0.0.1:8080/my_uri?with=args') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment