Skip to content

Commit 68e0c5c

Browse files
committed
make code easy to use or remove warnings
1 parent ce0be90 commit 68e0c5c

3 files changed

Lines changed: 32 additions & 12 deletions

File tree

functions/net/tcp/ssl/pyCheckCerts.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,43 @@
1313
https://globalsign.ssllabs.com/
1414
1515
"""
16+
import certifi
1617
import datetime
1718
import pprint
1819
import socket
1920
import ssl
2021
import time
2122

22-
import certifi
23+
try:
24+
from socket import errorTab
25+
except ImportError:
26+
errorTab = {
27+
10004: 'The operation was interrupted.',
28+
10009: 'A bad file handle was passed.',
29+
10013: 'Permission denied.',
30+
10014: 'A fault occurred on the network??',
31+
10022: 'An invalid operation was attempted.',
32+
10035: 'The socket operation would block',
33+
10036: 'A blocking operation is already in progress.',
34+
10048: 'The network address is in use.',
35+
10054: 'The connection has been reset.',
36+
10058: 'The network has been shut down.',
37+
10060: 'The operation timed out.',
38+
10061: 'Connection refused.',
39+
10063: 'The name is too long.',
40+
10064: 'The host is down.',
41+
10065: 'The host is unreachable.'
42+
}
2343

24-
soc = ssl.SSLSocket(socket.socket(),
25-
ca_certs=certifi.where(),
26-
cert_reqs=ssl.CERT_REQUIRED)
44+
# soc = ssl.SSLSocket(socket.socket(),
45+
# ca_certs=certifi.where(),
46+
# cert_reqs=ssl.CERT_REQUIRED)
47+
soc = ssl.wrap_socket(socket.socket(), ca_certs=certifi.where(), cert_reqs=ssl.CERT_REQUIRED) # type: ssl.SSLSocket
2748
try:
28-
soc.connect(("www.baidu.com", 443))
49+
soc.connect(("github.com", 443))
2950
except socket.error as e:
3051
# such as '10061', '[Errno 10061] Connection refused.'
31-
print(str(e), socket.errorTab.get(int(str(e).strip()[7:-1])))
52+
print(str(e), errorTab.get(int(str(e).strip()[7:-1])))
3253

3354
cert = soc.getpeercert()
3455
soc.close()

functions/net/tcp/ssl/pyCheckSSLCertificateStatus.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@
2626
Programming Language: Python :: 2.7
2727
Topic: Utilities
2828
"""
29+
import certifi
2930
import datetime
3031
import socket
3132
import ssl
3233

33-
import certifi
34-
3534

3635
def ssl_expiry_datetime(hostname):
3736
ssl_date_fmt = r'%b %d %H:%M:%S %Y %Z'
@@ -42,7 +41,7 @@ def ssl_expiry_datetime(hostname):
4241
server_hostname=hostname,
4342
)
4443
# 3 second timeout because Lambda has runtime limitations
45-
conn.settimeout(3.0)
44+
conn.settimeout(30.0)
4645

4746
conn.connect((hostname, 443))
4847
ssl_info = conn.getpeercert()

functions/net/tcp/ssl/pyGetCertsInfo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"""
1111
import datetime
1212
import time
13-
from socket import socket
14-
1513
from OpenSSL.SSL import Connection, Context, SSLv3_METHOD, TLSv1_2_METHOD
14+
from OpenSSL.SSL import X509Name
15+
from socket import socket
1616

1717
host = 'www.baidu.com'
1818

@@ -28,7 +28,7 @@
2828
c.set_connect_state()
2929
c.do_handshake()
3030
cert = c.get_peer_certificate()
31-
print "Issuer: ", cert.get_issuer()
31+
print "Issuer: ", cert.get_issuer() # type: X509Name
3232
print "Subject: ", cert.get_subject().get_components()
3333
subject_list = cert.get_subject().get_components()
3434
print "Common Name:", dict(subject_list).get("CN")

0 commit comments

Comments
 (0)