我正在嘗試爲域名中最左側的通配符編寫正則表達式。到目前爲止,我有這樣的:僅匹配域名中最左側的通配符 - Python
import re
o = urlparse(argv[1])
host_name = o.netloc
context = SSL.Context(SSL.TLSv1_METHOD) # Use TLS Method
context.set_options(SSL.OP_NO_SSLv2) # Don't accept SSLv2
context.set_verify(SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT,
callback)
# context.load_verify_locations(ca_file, ca_path)
sock = socket()
ssl_sock = SSL.Connection(context, sock)
ssl_sock.connect((host_name, 443))
ssl_sock.set_connect_state()
ssl_sock.set_tlsext_host_name(host_name)
ssl_sock.do_handshake()
cert = ssl_sock.get_peer_certificate()
common_name = cert.get_subject().commonName.decode()
print "Common Name: ", common_name
print "Cert number: ", cert.get_serial_number()
regex = common_name.replace('.', r'\.').replace('*',r'.*') + '$'
if re.match(regex, host_name):
print "matches"
else:
print "invalid"
# output:
Common Name: *.example.com
Cert number: 63694395280496902491340707875731768741
然而,正則表達式不僅*.example.com
,但*.*.*
或www.*.com
匹配。此外,https://wrong.host.example.com/
不應該被允許匹配。我怎樣才能確保它只匹配最左邊的標籤?
什麼是正確匹配的例子? – Alexander
* .example.com是一個有效的正確匹配 – cybertextron