Tous les produits
Search
Centre de documentation

Edge Security Acceleration:Exemples de signature d'URL

Dernière mise à jour :Sep 17, 2026

Démonstrations Python illustrant l'implémentation des trois types de signature d'URL (type A, type B et type C).

Démonstrations Python

Pour en savoir plus sur chaque type de signature d'URL, consultez :

Les blocs de code ci-dessous présentent les démonstrations.

Remarque
  • Python existe en deux versions majeures : Python 2 et Python 3. Python 3 n'est pas rétrocompatible avec Python 2. Des exemples de code sont donc fournis pour les deux versions.

  • Si une URL contient des caractères chinois, encodez-la à l'aide de la fonction UrlEncode() avant d'exécuter le code de signature d'URL.

  • Python 2 utilise l'encodage ASCII, tandis que Python 3 utilise l'encodage UTF-8. L'encodage UTF-8 est requis pour le hachage. L'exemple de code Python 3 ajoute donc l'encodage UTF-8 à la fonction hashlib.md5().

Python3

import re
import time
import hashlib
import datetime
def md5sum(src):
    m = hashlib.md5()
    m.update(src.encode(encoding='utf-8'))                                    # Add the UTF-8 encoding operation.
    return m.hexdigest()
    # Signing type A
def a_auth(uri, key, exp):
    p = re.compile("^(http://|https://)?([^/?]+)(/[^?]*)?(\\?.*)?$")
    if not p:
        return None
    m = p.match(uri)
    scheme, host, path, args = m.groups()
    if not scheme: scheme = "http://"
    if not path: path = "/"
    if not args: args = ""
    rand = "0"      # "0" by default, other value is ok
    uid = "0"       # "0" by default, other value is ok
    sstring = "%s-%s-%s-%s-%s" %(path, exp, rand, uid, key)
    hashvalue = md5sum(sstring)
    auth_key = "%s-%s-%s-%s" %(exp, rand, uid, hashvalue)
    if args:
        return "%s%s%s%s&auth_key=%s" %(scheme, host, path, args, auth_key)
    else:
        return "%s%s%s%s?auth_key=%s" %(scheme, host, path, args, auth_key)
    # Signing type B
def b_auth(uri, key, exp):
    p = re.compile("^(http://|https://)?([^/?]+)(/[^?]*)?(\\?.*)?$")
    if not p:
        return None
    m = p.match(uri)
    scheme, host, path, args = m.groups()
    if not scheme: scheme = "http://"
    if not path: path = "/"
    if not args: args = ""
    # convert unix timestamp to "YYmmDDHHMM" format
    nexp = datetime.datetime.fromtimestamp(exp).strftime('%Y%m%d%H%M')
    sstring = key + nexp + path
    hashvalue = md5sum(sstring)
    return "%s%s/%s/%s%s%s" %(scheme, host, nexp, hashvalue, path, args)
    # Signing type C
def c_auth(uri, key, exp):
    p = re.compile("^(http://|https://)?([^/?]+)(/[^?]*)?(\\?.*)?$")
    if not p:
        return None
    m = p.match(uri)
    scheme, host, path, args = m.groups()
    if not scheme: scheme = "http://"
    if not path: path = "/"
    if not args: args = ""
    hexexp = "%x" %exp
    sstring = key + path + hexexp
    hashvalue = md5sum(sstring)
    return "%s%s/%s/%s%s%s" %(scheme, host, hashvalue, hexexp, path, args)
    # The following section shows the values of the uri, key, and exp parameters.
def main():
    uri = "http://example.aliyundoc.com/ping?foo=bar"            # original uri
    key = "<input private key>"                         # private key of authorization
    exp = int(time.time()) + 1 * 3600                   # expiration time: 1 hour after current itme
    # "1 * 3600" specifies the time-to-live (TTL) value that the signing server assigns to signed URLs. You can customize the value as required. Unit: seconds. The TTL value that is assigned by the signing server is irrelevant to the TTL value that is assigned by DCDN. 
    # Validity period of a signed URL = UNIX timestamp that is generated on the signing server + TTL that is assigned by the signing server + TTL that is assigned by DCDN.
    # For signing type A, if the UNIX timestamp that is generated on the signing server is 1444435200, the TTL value that is assigned by the signing server is 3600, and the TTL value that is assigned by DCDN is 1800, then the validity period of the URL is 1444440600 (1444435200 + 3600 + 1800).
    # The following section shows how to implement signing type A:
    authuri = a_auth(uri, key, exp)                     # auth type: a_auth / b_auth / c_auth
    print("URL : %s\nAUTH: %s" %(uri, authuri))
if __name__ == "__main__":
    main()

Python2

import re
import time
import hashlib
import datetime
def md5sum(src):
    m = hashlib.md5()
    m.update(src)
    return m.hexdigest()
    # Signing type A
def a_auth(uri, key, exp):
    p = re.compile("^(http://|https://)?([^/?]+)(/[^?]*)?(\\?.*)?$")
    if not p:
        return None
    m = p.match(uri)
    scheme, host, path, args = m.groups()
    if not scheme: scheme = "http://"
    if not path: path = "/"
    if not args: args = ""
    rand = "0"      # "0" by default, other value is ok
    uid = "0"       # "0" by default, other value is ok
    sstring = "%s-%s-%s-%s-%s" %(path, exp, rand, uid, key)
    hashvalue = md5sum(sstring)
    auth_key = "%s-%s-%s-%s" %(exp, rand, uid, hashvalue)
    if args:
        return "%s%s%s%s&auth_key=%s" %(scheme, host, path, args, auth_key)
    else:
        return "%s%s%s%s?auth_key=%s" %(scheme, host, path, args, auth_key)
    # Signing type B
def b_auth(uri, key, exp):
    p = re.compile("^(http://|https://)?([^/?]+)(/[^?]*)?(\\?.*)?$")
    if not p:
        return None
    m = p.match(uri)
    scheme, host, path, args = m.groups()
    if not scheme: scheme = "http://"
    if not path: path = "/"
    if not args: args = ""
    # convert unix timestamp to "YYmmDDHHMM" format
    nexp = datetime.datetime.fromtimestamp(exp).strftime('%Y%m%d%H%M')
    sstring = key + nexp + path
    hashvalue = md5sum(sstring)
    return "%s%s/%s/%s%s%s" %(scheme, host, nexp, hashvalue, path, args)
    # Signing type C
def c_auth(uri, key, exp):
    p = re.compile("^(http://|https://)?([^/?]+)(/[^?]*)?(\\?.*)?$")
    if not p:
        return None
    m = p.match(uri)
    scheme, host, path, args = m.groups()
    if not scheme: scheme = "http://"
    if not path: path = "/"
    if not args: args = ""
    hexexp = "%x" %exp
    sstring = key + path + hexexp
    hashvalue = md5sum(sstring)
    return "%s%s/%s/%s%s%s" %(scheme, host, hashvalue, hexexp, path, args)
    # The following section shows the values of the uri, key, and exp parameters.
def main():
    uri = "http://example.aliyundoc.com/ping?foo=bar"            # original uri
    key = "<input private key>"                         # private key of authorization
    exp = int(time.time()) + 1 * 3600                   # expiration time: 1 hour after current itme
    # "1 * 3600" specifies the time-to-live (TTL) value that the signing server assigns to signed URLs. You can customize the value as required. Unit: seconds. The TTL value that is assigned by the signing server is irrelevant to the TTL value that is assigned by DCDN. 
    # Validity period of a signed URL = UNIX timestamp that is generated on the signing server + TTL that is assigned by the signing server + TTL that is assigned by DCDN.
    # For signing type A, if the UNIX timestamp that is generated on the signing server is 1444435200, the TTL value that is assigned by the signing server is 3600, and the TTL value that is assigned by DCDN is 1800, then the validity period of the URL is 1444440600 (1444435200 + 3600 + 1800).
    # The following section shows how to implement signing type A:
    authuri = a_auth(uri, key, exp)                     # auth type: a_auth / b_auth / c_auth
    print("URL : %s\nAUTH: %s" %(uri, authuri))
if __name__ == "__main__":
    main()