def shorten_url(long_url):
global counter
short_code = hashlib.md5(long_url.encode()).hexdigest()[:6] 取前6位MD5
if short_code not in url_map:
url_map[short_code] = long_url
return fhttp://short.url/{short_code}
def expand_url(short_code):
return url_map.get(short_code, URL not found)
示例
long_url = https://www.example.com/reallylongpath?query=param
short = shorten_url(long_url)
print(短链接:, short)