Created
August 4, 2018 17:02
-
-
Save wolfhong/f66a826b780686c2c7d13e11fac81dd5 to your computer and use it in GitHub Desktop.
使用代理服务器进行网络请求的例子
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 python | |
# -*- coding: utf-8 -*- | |
''' | |
proxy data: | |
http://cn-proxy.com/ | |
http://cn-proxy.com/archives/218 | |
''' | |
from __future__ import unicode_literals | |
import os | |
import requests | |
HTTP_PROXY = 'http://221.130.253.135:8090' | |
def get_pages(*args): | |
os.environ.setdefault('HTTP_PROXY', HTTP_PROXY) | |
headers = { | |
'user-agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36', | |
} | |
with requests.Session() as s: | |
# s.auth = ('user', 'password') | |
for url in args: | |
r = s.get(url, timeout=5, headers=headers) | |
print(r.text) | |
if __name__ == '__main__': | |
get_pages('http://47.75.91.140:8080/') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment