Python使用指定端口進行http請求的例子
更新時間:2019年07月25日 11:35:29 作者:ishouyong
今天小編就為大家分享一篇Python使用指定端口進行http請求的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
使用requests庫
class SourcePortAdapter(HTTPAdapter): """"Transport adapter" that allows us to set the source port.""" def __init__(self, port, *args, **kwargs): self.poolmanager = None self._source_port = port super().__init__(*args, **kwargs) def init_poolmanager(self, connections, maxsize, block=DEFAULT_POOLBLOCK, **pool_kwargs): self.poolmanager = PoolManager( num_pools=connections, maxsize=maxsize, block=block, source_address=('', self._source_port)) s = requests.Session() s.mount('https://baidu.com', SourcePortAdapter(54321)) s.get('https://baidu.com')
我用wireshark測試發(fā)現(xiàn)是走的54321端口。
使用pycurl庫
c = pycurl.Curl() c.setopt(c.URL, 'https://curl.haxx.se/dev/') c.setopt(c.LOCALPORT, 54321) c.setopt(c.LOCALPORTRANGE, [52314,56321,5532]) c.perform() c.close()
測試OK,可以直接在curl命令行中測試。
curl --local-port 12520 http://baidu.com
參考
以上這篇Python使用指定端口進行http請求的例子就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- python+requests+pytest接口自動化的實現(xiàn)示例
- Python+unittest+requests+excel實現(xiàn)接口自動化測試框架
- Python+unittest+requests 接口自動化測試框架搭建教程
- Python3+Requests+Excel完整接口自動化測試框架的實現(xiàn)
- python獲取http請求響應(yīng)頭headers中的數(shù)據(jù)的示例
- 基于Python模擬瀏覽器發(fā)送http請求
- 如何基于Python + requests實現(xiàn)發(fā)送HTTP請求
- 對Python發(fā)送帶header的http請求方法詳解
- python接口自動化使用requests庫發(fā)送http請求
相關(guān)文章
Python將string轉(zhuǎn)換到float的實例方法
在本篇文章中小編給大家分享的是關(guān)于Python將string轉(zhuǎn)換到float的實例方法以及相關(guān)知識點,需要的朋友們參考下。2019-07-07解讀MaxPooling1D和GlobalMaxPooling1D的區(qū)別
這篇文章主要介紹了MaxPooling1D和GlobalMaxPooling1D的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12