Skip to content

Instantly share code, notes, and snippets.

@Shu-Ji
Last active August 29, 2015 14:04
Show Gist options
  • Save Shu-Ji/938b0f57924b9bb49652 to your computer and use it in GitHub Desktop.
Save Shu-Ji/938b0f57924b9bb49652 to your computer and use it in GitHub Desktop.
简单的支持cookie和代理的网络请求封装
# coding: u8
import urllib2
import urllib
import cookielib
class Net(object):
def __init__(self, cookie_path=None, encoding='u8', timeout=120,
proxy_host=None, proxy_port=None):
self.timeout = timeout
self.encoding = encoding
self.cookie = cookielib.LWPCookieJar(cookie_path)
if cookie_path is not None:
try:
self.cookie.load(ignore_discard=True, ignore_expires=True)
except Exception:
self.cookie.save(cookie_path,
ignore_discard=True, ignore_expires=True)
self.cookie.get = self.get_cookie
self.headers = {
'User-Agent': 'Mozilla/5.0 (Linux) AppleWebKit/537.1 Chrome/21'
}
cookie_support = urllib2.HTTPCookieProcessor(self.cookie)
if proxy_host is not None:
proxy_support = urllib2.ProxyHandler({
'http': '%s:%s' % (proxy_host, proxy_port),
'https': '%s:%s' % (proxy_host, proxy_port),
})
self.opener = urllib2.build_opener(proxy_support, cookie_support)
else:
self.opener = urllib2.build_opener(cookie_support)
def update_headers(self, *args, **kwargs):
self.headers.update(dict(*args, **kwargs))
def post(self, url, params):
params = urllib.urlencode(params)
self.opener.addheaders = self.headers.items()
return self.opener.open(urllib2.Request(url, params),
timeout=self.timeout).read().decode(self.encoding)
def get(self, url):
self.opener.addheaders = self.headers.items()
return self.opener.open(urllib2.Request(url),
timeout=self.timeout).read().decode(self.encoding)
def get_cookie(self, name):
for i in self.cookie:
if i.name == name:
return i.value
if __name__ == '__main__':
n = Net()
print n.get('http://www.baidu.com/')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment