使用Ruby开发的Bot出现503 Service Unavailable异常求助
先梳理下你的核心问题:之前运行正常的Yobit Bot突然无法连接目标网站,但手动访问网页完全没问题;你已经尝试过换VPN、降级Ruby版本,还发现RubyGems版本显示不一致的情况,错误日志明确报出RestClient::ServiceUnavailable (503 Service Unavailable)。结合这些信息,我给你整理几个针对性的排查和修复方向:
1. 优先排查请求头差异(最可能的拦截原因)
很多网站会通过请求头识别非浏览器请求,直接拒绝Bot访问。你手动访问用的是浏览器,而rest-client默认发送的请求头过于“简洁”,很容易被反爬机制拦截。
修改yobit_bot.rb里的call_api方法,给rest-client的GET请求加上模拟浏览器的请求头:
RestClient.get(your_api_endpoint, { 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36', 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Language' => 'en-US,en;q=0.5' })
添加后再测试Bot是否能正常连接。
2. 修复RubyGems版本不一致问题
日志显示Bot使用的是RubyGems 2.3.0,但gem update --system输出2.7.4,这种版本混乱很可能导致依赖包兼容性问题。你可以按以下步骤修复:
- 先确认当前终端生效的gem版本:执行
gem --version,看实际运行环境用的是哪个版本 - 卸载现有冲突的依赖包,重新安装对应Ruby23的版本:
gem uninstall rest-client bundler gem install bundler -v 1.16.0 gem install rest-client -v 2.0.2 - 最后执行
bundle install重新同步依赖,确保环境版本统一。
3. 确认VPN代理是否被Bot正确使用
你换了VPN,但Ruby程序默认不会自动继承系统代理设置,可能Bot依然在用原IP访问,导致被网站封禁。可以做这两步验证:
- 检查VPN是否开启全局代理模式(让所有系统程序走VPN流量)
- 或者在代码里手动指定代理地址(从VPN客户端获取代理IP和端口):
RestClient.proxy = 'http://your-vpn-proxy-ip:port'
4. 验证API端点是否正常
手动访问的是网站首页,但Bot调用的是API接口,有可能是API端点临时维护或路径变更。你可以:
- 用curl或Postman手动调用Bot里的API地址(对应
yobit_bot.rb第57行的接口),看返回结果 - 如果手动调用API也返回503,那就是网站API的问题;如果手动调用正常,那还是Bot请求的参数/头信息有问题,回到第一步继续排查。
附你的错误日志:
C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rest-client-2.0.2-x64-mingw32/lib/restclient/abstract_response.rb:223:in `exception_with_response': 503 Service Unavailable (RestClient::ServiceUnavailable) from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rest-client-2.0.2-x64-mingw32/lib/restclient/abstract_response.rb:103:in `return!' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rest-client-2.0.2-x64-mingw32/lib/restclient/request.rb:809:in `process_result' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rest-client-2.0.2-x64-mingw32/lib/restclient/request.rb:725:in `block in transmit' from C:/Ruby23-x64/lib/ruby/2.3.0/net/http.rb:853:in `start' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rest-client-2.0.2-x64-mingw32/lib/restclient/request.rb:715:in `transmit' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rest-client-2.0.2-x64-mingw32/lib/restclient/request.rb:145:in `execute' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rest-client-2.0.2-x64-mingw32/lib/restclient/request.rb:52:in `execute' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rest-client-2.0.2-x64-mingw32/lib/restclient.rb:67:in `get' from yobit_bot.rb:57:in `call_api' from yobit_bot.rb:85:in `get_market_summary' from yobit_bot.rb:171:in `buy_sell_bot' from yobit_bot.rb:228:in `<main>'
内容的提问来源于stack exchange,提问作者G F
相关产品推荐
相关产品推荐

