添加代理支持(使用 pycurl)

This commit is contained in:
lilydjwg 2013-07-02 17:50:45 +08:00
parent 4d75b63c95
commit 15fb385235
3 changed files with 26 additions and 1 deletions

View File

@ -61,6 +61,9 @@ regex
When multiple version strings are found, the maximum of those is chosen. When multiple version strings are found, the maximum of those is chosen.
proxy
The HTTP proxy to use. The format is ``host:port``, e.g. ``localhost:8087``.
Find with a Command Find with a Command
------------------- -------------------
Use a shell command line to get the version. The output is striped first, so trailing newlines do not bother. Use a shell command line to get the version. The output is striped first, so trailing newlines do not bother.

View File

@ -4,6 +4,7 @@ import logging
from functools import partial from functools import partial
import queue import queue
import json import json
import urllib.parse
from pkg_resources import parse_version from pkg_resources import parse_version
from tornado.httpclient import AsyncHTTPClient from tornado.httpclient import AsyncHTTPClient
@ -13,6 +14,12 @@ from tornado.ioloop import IOLoop
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
handler_precedence = ('github', 'aur', 'cmd', 'regex') handler_precedence = ('github', 'aur', 'cmd', 'regex')
try:
import pycurl
AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient")
except ImportError:
pycurl = None
def get_version(name, conf, callback): def get_version(name, conf, callback):
g = globals() g = globals()
for key in handler_precedence: for key in handler_precedence:
@ -34,9 +41,19 @@ def get_version_by_regex(name, conf, callback):
encoding = conf.get('encoding', 'latin1') encoding = conf.get('encoding', 'latin1')
httpclient = AsyncHTTPClient() httpclient = AsyncHTTPClient()
kwargs = {}
if conf.get('proxy'):
if pycurl:
host, port = urllib.parse.splitport(conf['proxy'])
kwargs['proxy_host'] = host
kwargs['proxy_port'] = int(port)
else:
logger.warn('%s: proxy set but not used because pycurl is unavailable.', name)
httpclient.fetch(conf['url'], partial( httpclient.fetch(conf['url'], partial(
_get_version_by_regex, name, r, encoding, callback _get_version_by_regex, name, r, encoding, callback
)) ), **kwargs)
def _get_version_by_regex(name, regex, encoding, callback, res): def _get_version_by_regex(name, regex, encoding, callback, res):
body = res.body.decode(encoding) body = res.body.decode(encoding)

View File

@ -24,3 +24,8 @@ github = lilydjwg/winterpy
[nvchecker] [nvchecker]
github = lilydjwg/nvchecker github = lilydjwg/nvchecker
[ssed]
url = http://sed.sourceforge.net/grabbag/ssed/
regex = The current version is ([\d.]+)\.
proxy = localhost:8087