我如何通过python使用用户名和密码制作SSl Connection服务器?

时间:2018-11-15 13:23:01

标签: python ssl server

您好,我想问一下我是否可以进行连接以从Tool Server中提取一些数据。

所以我要编写此代码,但我需要对其进行修改以使用用户名和密码打开此工具,并从“我的工具”服务器中提取数据。

import socket
import ssl

HOST, PORT = '10.74.159.82', 31039

def handle(conn):
    conn.write(b'GET  /  HTTP/1.1\n')
    print(conn.recv() . decode())

def main():
    sock = socket.socket(socket.AF_INET) 
    context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
    conn = context.wrap_socket(sock, server_hostname=HOST)
    try:
        conn.connect((HOST, PORT))
        handle(conn)
    finally:
        conn.close()

if __name__ == '__main__':
    main()
结果
       !! C:\Users\Admin\.PyCharmCE2018.1\config\venv\Scripts\python.exe!! C:/Users/Admin/.PyCharmCE2018.1/config/codestyles/Under_Building.py
Traceback (most recent call last):
  File "C:/Users/Admin/.PyCharmCE2018.1/config/codestyles/Under_Building.py", line 22, in <module>
    main()
  File "C:/Users/Admin/.PyCharmCE2018.1/config/codestyles/Under_Building.py", line 16, in main
    conn.connect((HOST, PORT))
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 1141, in connect
    self._real_connect(addr, False)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 1132, in _real_connect
    self.do_handshake()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 1108, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1045)
Process finished with exit code 1

我是初学者,所以我需要学习和支持

1 个答案:

答案 0 :(得分:0)

很难确切看到,但是似乎您正在连接的服务器(在https://10.74.159.82:31039上)正在使用self-signed certificate(或者它的CA可能是)。

创建或购买适当的(非自签名)证书可能更好,但是假设这是测试服务器,就可以了。

因此,最好的选择是在SSL上下文中允许自签名证书(并且不要检查主机名-在使用IP地址时可能也会失败):

context = ssl.create_default_context()
context.check_hostname=False
context.verify_mode=ssl.CERT_NONE
# etc...