You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ibapi Python连接本地IB Gateway遇断开错误,请求技术支持

问题描述

我在本地运行IB Gateway客户端,需要调用账户详情、订单执行、查看订单等基础API。使用ibapi作为IB API的Python包装器时出现连接错误:

>>> from ibapi.client import EClient
>>> from ibapi.wrapper import EWrapper  
>>> class IBapi(EWrapper, EClient):
...      def __init__(self):
...          EClient.__init__(self, self) 
... 
>>> app = IBapi()
>>> app.connect('127.0.0.1', 7497, 123534)
Disconnected; resetting connection

IB Gateway控制台输出:

runtime path : 

root:dist/ibgroup.web.core.iblink.router.clientportal.gw.jar:build/lib/runtime/*
 config file  : root/conf.yaml
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by io.netty.util.internal.ReflectionUtil (file:/home/parkar10/clientportal-gw/build/lib/runtime/netty-common-4.1.15.Final.jar) to constructor java.nio.DirectByteBuffer(long,int)
WARNING: Please consider reporting this to the maintainers of io.netty.util.internal.ReflectionUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
 -> mount demo on /demo
Java Version: 11.0.16
****************************************************
version: bae7d315fc7f01dbb300e6d20379b83d9d590689 Wed, 7 Jul 2021 18:41:40 -0400
****************************************************
This is the Client Portal Gateway
for any issues, please contact api@ibkr.com
and include a copy of your logs
****************************************************
https://www.interactivebrokers.com/api/doc.html
****************************************************
Open https://localhost:7497 to login
App demo is available after you login under: https://localhost:7497/demo#/

日志内容:

14:12:10.689 WARN  vert.x-eventloop-thread-0 DefaultChannelPipeline : An 

exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
io.netty.handler.codec.DecoderException: io.netty.handler.ssl.NotSslRecordException: not an SSL/TLS record: 4150490000000009763130302e2e313537
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:459)
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:265)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
    at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1359)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:935)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:134)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:645)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:580)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:497)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:459)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858)
    at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: io.netty.handler.ssl.NotSslRecordException: not an SSL/TLS record: 4150490000000009763130302e2e313537
    at io.netty.handler.ssl.SslHandler.decodeJdkCompatible(SslHandler.java:1106)
    at io.netty.handler.ssl.SslHandler.decode(SslHandler.java:1162)
    at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:489)
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:428)
    ... 15 common frames omitted
解决方案

日志中的io.netty.handler.ssl.NotSslRecordException明确指向问题核心:API类型与端口不兼容。

你当前运行的是Client Portal Gateway(控制台输出已标注),它的7497端口是用于REST API的HTTPS端口,但ibapi是专为传统TWS/IB Gateway的TCP协议API设计的,两者无法直接对接。

解决办法分两种情况:

继续使用ibapi

  1. 卸载当前的Client Portal Gateway,下载安装传统版IB Gateway。
  2. 传统IB Gateway的默认API端口:实盘用7496,模拟盘用4002,修改代码中的连接端口:
    app.connect('127.0.0.1', 4002, 123534)  # 模拟盘示例,实盘替换为7496
    
  3. 打开传统IB Gateway的设置,开启"Active X and Socket Clients"选项,允许本地IP连接,并确认端口号与代码一致。

坚持使用Client Portal Gateway

放弃ibapi,改用它对应的REST API进行接口调用,或者使用官方适配的HTTP客户端库。

内容的提问来源于stack exchange,提问作者akshay parkar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 22:00:59