在 Python 中打开 SSL 套接字连接 [英] Opening a SSL socket connection in Python

查看:62
本文介绍了在 Python 中打开 SSL 套接字连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Python 中建立一个安全的套接字连接,但我很难使用它的 SSL 位.我找到了一些关于如何与 SSL 建立连接的代码示例,但它们都涉及密钥文件.我尝试连接的服务器不需要接收任何密钥或证书.我的问题是我如何本质上用 SSL 包装 python 套接字连接.我知道我假设使用的密码是 ADH-AES256-SHA,协议是 TLSv1.这就是我一直在尝试的:

I'm trying to establish a secure socket connection in Python, and i'm having a hard time with the SSL bit of it. I've found some code examples of how to establish a connection with SSL, but they all involve key files. The server i'm trying to connect with doesn't need to receive any keys or certificates. My question is how do I essentially wrap a python socket connection with SSL. I know for a fact that the cipher i'm suppose to use is ADH-AES256-SHA, and the protocol is TLSv1. This is what i've been trying:

import socket
import ssl

# SET VARIABLES
packet, reply = "<packet>SOME_DATA</packet>", ""
HOST, PORT = 'XX.XX.XX.XX', 4434

# CREATE SOCKET
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(10)

# WRAP SOCKET ???
ssl.wrap_socket(sock, ssl_version="TLSv1", ciphers="ADH-AES256-SHA")

# CONNECT AND PRINT REPLY
sock.connect((HOST, PORT))
sock.send(packet)
print sock.recv(1280)

# CLOSE SOCKET CONNECTION
sock.close()

当我运行此代码时,我没有收到任何错误,但收到空白响应.当尝试在命令行中调试此代码时,通过在终端中键入 python 并逐行粘贴代码,我得到了运行 sock 时假设的状态代码.发送(数据包).我得到的整数响应是 26.如果有人知道这意味着什么,或者可以提供帮助,我们将不胜感激.提前致谢!

When I run this code, I don't get any errors, but I get a blank response. When trying to debug this code in the command line, by typing in python in the terminal and pasting in code line by line, I get what i'm assuming is a status code when running sock.send(packet). The integer response I get is 26. If anyone knows what this means, or can help in anyway it would be greatly appreciated. Thanks in advance!

推荐答案

好的,我知道出了什么问题.这对我来说有点愚蠢.我的代码有两个问题.我的第一个错误是在指定 ssl_version 时我放入了 TLSv1,而它应该是 ssl.PROTOCOL_TLSv1.第二个错误是我没有引用包装好的套接字,而是调用了我创建的原始套接字.下面的代码似乎对我有用.

Ok, I figured out what was wrong. It was kind of foolish of me. I had two problems with my code. My first mistake was when specifying the ssl_version I put in TLSv1 when it should have been ssl.PROTOCOL_TLSv1. The second mistake was that I wasn't referencing the wrapped socket, instead I was calling the original socket that I have created. The below code seemed to work for me.

import socket
import ssl

# SET VARIABLES
packet, reply = "<packet>SOME_DATA</packet>", ""
HOST, PORT = 'XX.XX.XX.XX', 4434

# CREATE SOCKET
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(10)

# WRAP SOCKET
wrappedSocket = ssl.wrap_socket(sock, ssl_version=ssl.PROTOCOL_TLSv1, ciphers="ADH-AES256-SHA")

# CONNECT AND PRINT REPLY
wrappedSocket.connect((HOST, PORT))
wrappedSocket.send(packet)
print wrappedSocket.recv(1280)

# CLOSE SOCKET CONNECTION
wrappedSocket.close()

希望这可以帮助某人!

这篇关于在 Python 中打开 SSL 套接字连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆