httpd: improve api error handling
This commit is contained in:
11
httpd.py
11
httpd.py
@@ -56,7 +56,7 @@ class ProxyAPIHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||||||
"""Show available endpoints."""
|
"""Show available endpoints."""
|
||||||
endpoints = {
|
endpoints = {
|
||||||
'endpoints': {
|
'endpoints': {
|
||||||
'/proxies': 'list working proxies (params: limit, proto, country)',
|
'/proxies': 'list working proxies (params: limit, proto, country, asn)',
|
||||||
'/proxies/count': 'count working proxies',
|
'/proxies/count': 'count working proxies',
|
||||||
'/health': 'health check',
|
'/health': 'health check',
|
||||||
}
|
}
|
||||||
@@ -77,10 +77,11 @@ class ProxyAPIHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||||||
limit = min(int(params.get('limit', 100)), 1000)
|
limit = min(int(params.get('limit', 100)), 1000)
|
||||||
proto = params.get('proto', '')
|
proto = params.get('proto', '')
|
||||||
country = params.get('country', '')
|
country = params.get('country', '')
|
||||||
|
asn = params.get('asn', '')
|
||||||
format = params.get('format', 'json')
|
format = params.get('format', 'json')
|
||||||
|
|
||||||
# Build query
|
# Build query
|
||||||
sql = 'SELECT ip, port, proto, country FROM proxylist WHERE failed=0'
|
sql = 'SELECT ip, port, proto, country, asn FROM proxylist WHERE failed=0'
|
||||||
args = []
|
args = []
|
||||||
|
|
||||||
if proto:
|
if proto:
|
||||||
@@ -89,6 +90,9 @@ class ProxyAPIHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||||||
if country:
|
if country:
|
||||||
sql += ' AND country=?'
|
sql += ' AND country=?'
|
||||||
args.append(country.upper())
|
args.append(country.upper())
|
||||||
|
if asn:
|
||||||
|
sql += ' AND asn=?'
|
||||||
|
args.append(int(asn))
|
||||||
|
|
||||||
sql += ' ORDER BY tested DESC LIMIT ?'
|
sql += ' ORDER BY tested DESC LIMIT ?'
|
||||||
args.append(limit)
|
args.append(limit)
|
||||||
@@ -109,7 +113,8 @@ class ProxyAPIHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||||||
'ip': row[0],
|
'ip': row[0],
|
||||||
'port': row[1],
|
'port': row[1],
|
||||||
'proto': row[2],
|
'proto': row[2],
|
||||||
'country': row[3]
|
'country': row[3],
|
||||||
|
'asn': row[4]
|
||||||
})
|
})
|
||||||
self.send_json({'count': len(proxies), 'proxies': proxies})
|
self.send_json({'count': len(proxies), 'proxies': proxies})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user