self_example/Spider/Chapter09_代理的使用/代理的设置/aiohttpDemo.py

41 lines
972 B
Python

# -*- encoding:utf-8 -*-
'''
@Author : dingjiawen
@Date : 2023/12/13 19:54
@Usage :
@Desc :
'''
import asyncio
import aiohttp
# 普通http
proxy = 'http://127.0.0.1:7890'
# http_auth
proxy = 'http://username:password@127.0.0.1:7890'
async def main():
async with aiohttp.ClientSession() as session:
async with session.get('https://httpbin.org/get', proxy=proxy) as response:
print(await response.text())
async def socks():
from aiohttp_socks import ProxyConnector, ProxyType
connector = ProxyConnector(
proxy_type=ProxyType.HTTP,
host='127.0.0.1',
port=7890,
# username='user',
# password='password',
# rdns=True
)
async with aiohttp.ClientSession(connector=connector) as session:
async with session.get('https://httpbin.org/get') as response:
print(await response.text())
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(main())