Python内置ssl模块编程(1)

2020-01-06 11:20:18 浏览数 (1)

1.配置IIS

2.客户端安装SSL访问web服务器

代码语言:javascript复制
#!/usr/bin/env python
import socket, sys

def sendall(s, buf):
    byteswritten = 0
    while byteswritten < len(buf):
        byteswritten  = s.write(buf[byteswritten:])

print "Creating socket...",
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "done."

print "Connecting to remote host...",
s.connect(("127.0.0.1", 443))
print "done."

print "Establishing SSL...",
ssl = socket.ssl(s)
print "done."

print "Requesting document...",
sendall(ssl, "GET / HTTP/1.0rnrn")
print "done."

s.shutdown(1)

while 1:
    try:
        buf = ssl.read(1024)
    except socket.sslerror, err:
        if (err[0]) in [socket.SSL_ERROR_ZERO_RETURN, socket.SSL_ERROR_EOF]:
            break
        elif (err[0]) in [socket.SSL_ERROR_WANT_READ,
                          socket.SSL_ERROR_WANT_WRITE]:
            continue
        raise
    if len(buf) == 0:
        break
    sys.stdout.write(buf)

s.close()

0 人点赞