代码示例-Python
发布时间:2021-08-03 21:04
Python requests
import requests
targetURL = "https://ip.cn/api/index?ip=&type=0"
proxyAddr = "您的代理IP:端口"
authKey = "请改成您的Key"
password = "请改成您的AuthPwd"
# 账密模式
proxyUrl = "http://%(user)s:%(password)s@%(server)s" % {
"user": authKey,
"password": password,
"server": proxyAddr,
}
proxies = {
"http": proxyUrl,
"https": proxyUrl,
}
resp = requests.get(targetURL, proxies=proxies)
print(resp.text)
Python aiohttp
import aiohttp,asyncio
targetURL = "https://ip.cn/api/index?ip=&type=0"
proxyAddr = "您的代理IP:端口"
authKey = "请改成您的Key"
password = "请改成您的AuthPwd"
# 账密模式
proxyUrl = "http://%(user)s:%(password)s@%(server)s" % {
"user": authKey,
"password": password,
"server": proxyAddr,
}
async def entry():
conn = aiohttp.TCPConnector(ssl=False)
async with aiohttp.ClientSession(connector=conn) as session:
async with session.get(targetURL, proxy=proxyUrl) as resp:
body = await resp.read()
print(resp.status)
print(body)
loop = asyncio.get_event_loop()
loop.run_until_complete(entry())
loop.run_forever()
Python urllib2、urllib
import urllib2
targetURL = "https://ip.cn/api/index?ip=&type=0"
proxyAddr = "您的代理IP:端口"
authKey = "请改成您的Key"
password = "请改成您的AuthPwd"
proxyUrl = "http://%(user)s:%(password)s@%(server)s" % {
"user": authKey,
"password": password,
"server": proxyAddr,
}
proxies = urllib2.ProxyHandler({
"http": proxyUrl,
"https": proxyUrl,
})
opener = urllib2.build_opener(proxies)
urllib2.install_opener(opener)
resp = urllib2.urlopen(targetURL).read()
print(resp)
相关文章