
本文档旨在指导开发者如何使用 Python QuickFIX 库通过 Stunnel 建立安全的 FIX (Financial Information eXchange) 连接。我们将详细介绍 Stunnel 的配置、QuickFIX 的设置,以及如何调试可能出现的问题,确保 FIX 消息能够安全可靠地传输。
Stunnel 是一个通用的 TLS/SSL 加密隧道程序,可以用于为不支持 SSL 的客户端和服务端提供安全的加密连接。以下是一个 Stunnel 客户端配置文件的示例,用于连接到 FIX 服务器:
CAfile=./Roots.crt cert=./cert.pfx client=yes verify=1 sslVersion = TLSv1.2 options = NO_SSLv2 options = NO_SSLv3 debug=7 foreground=yes output=./stunnel.log [SESSION] accept=8080 connect=EXAMPLE_URL:EXAMPLE_IP
配置参数说明:
注意事项:
立即学习“Python免费学习笔记(深入)”;
QuickFIX 是一个开源的 FIX 协议引擎,提供多种编程语言的 API。以下是一个 QuickFIX Initiator 配置文件的示例:
[DEFAULT] ConnectionType=initiator LogonTimeout=10 ReconnectInterval=30 FileLogPath=./Logs/ [SESSION] BeginString=FIXT.1.1 DefaultApplVerID=9 SenderCompID=EXAMPLE_COMP_SENDER TargetCompID=EXAMPLE_COMP_TARGET StartTime=00:00:00 EndTime=00:00:00 HeartBtInt=30 CheckLatency=N MaxLatency=240 SocketConnectPort=8080 SocketConnectHost=127.0.0.1 UseDataDictionary=Y TransportDataDictionary=./FIX50SP2.xml FileStorePath=./Sessions/
配置参数说明:
Python 代码示例:
import quickfix as fix
class Application(fix.Application):
def onCreate(self, sessionID):
print("onCreate : Session (%s)" % sessionID.toString())
def onLogon(self, sessionID):
self.sessionID = sessionID
print("Successful Logon to session '%s'." % sessionID.toString())
def onLogout(self, sessionID):
print("onLogout")
def toAdmin(self, sessionID, message):
print("toAdmin")
def fromAdmin(self, sessionID, message):
print("fromAdmin")
def toApp(self, sessionID, message):
print("Recieved the following message: %s" % message.toString())
def fromApp(self, message, sessionID):
print("fromApp")
config_file = "./initiator.cfg"
settings = fix.SessionSettings(config_file)
application = Application()
storeFactory = fix.FileStoreFactory(settings)
logFactory = fix.FileLogFactory(settings)
initiator = fix.SocketInitiator(application, storeFactory, settings, logFactory)
initiator.start()代码说明:
如果在连接过程中遇到问题,可以按照以下步骤进行调试:
QuickFIX 会自动处理心跳消息。如果需要手动发送心跳请求,可以在 Application 类中添加以下代码:
import time
def onLogon(self, sessionID):
self.sessionID = sessionID
print("Successful Logon to session '%s'." % sessionID.toString())
# 定时发送心跳请求
while True:
time.sleep(self.heartbeat_interval)
try:
fix.Session.sendHeartbeat(sessionID)
print("Heartbeat sent")
except fix.SessionNotFound:
print("Session not found, stopping heartbeat")
break注意事项:
立即学习“Python免费学习笔记(深入)”;
通过本文档的指导,你应该能够成功地使用 Python QuickFIX 库通过 Stunnel 建立安全的 FIX 连接。记住,仔细检查配置、查看日志,并使用调试工具可以帮助你解决连接过程中遇到的问题。
以上就是使用 Python QuickFIX 通过 Stunnel 连接 FIX 服务器的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号