#!/usr/bin/python3 import json import sqlite3 import urllib.request # PyPI API documentation https://warehouse.readthedocs.io/api-reference/ PyPI_base = "https://pypi.org/pypi/{}/json" def get_version_from_pypi(name, etag=None): req = urllib.request.Request(url=PyPI_base.format(name)) if etag is not None: req.add_header('ETag', etag) with urllib.request.urlopen(req) as resp: if resp.getcode() == 200: data = json.load(resp) info_dict = data['info'] return info_dict['name'], info_dict['version'], resp.info()['ETag'] else: IOError(resp.info())