RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 572546
Accepted
mymedia
mymedia
Asked:2020-10-01 22:52:58 +0000 UTC2020-10-01 22:52:58 +0000 UTC 2020-10-01 22:52:58 +0000 UTC

HTTPS 请求的持久连接

  • 772

在网上,我找到了几个如何发出 HTTPS 请求的示例。然而,它们都有一个非常严重的缺点:它们在从服务器接收到所有数据后立即关闭 TCP 连接。这不好。

这是一个例子

https = require('ssl.https')
local data, code = https.request('https://httpbin.org/get')

如何以最小的痛苦来补充它,以便连接不会关闭?

lua
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    zed
    2020-10-05T04:00:01Z2020-10-05T04:00:01Z

    该库LuaSocket(在示例代码中使用)不支持持久性 http(s) 连接。因此,不能免除最小痛苦,必须使用其他一些库。

    例如,您可以使用Lua-cURL:

    local curl = require "cURL"
    
    local function main()
    
      local e = curl.easy()
    
      -- включает подробный вывод о ходе соединения
      e:setopt_verbose(true)
    
      e:setopt_url("https://httpbin.org/get")
      e:perform() -- выполняет загрузку и НЕ закрывает соединение
    
      -- отключает переиспользование соединений
      e:setopt_forbid_reuse(true)
    
      e:setopt_url("https://httpbin.org/ip")
      e:perform() -- выполняет загрузку и закрывает соединение
    
      e:close()
    end
    
    main()
    

    相反,在这个库中,默认情况下连接不会关闭并被重用(即使在调用之后e:close),在这里您需要使用特殊的CURLOPT_FORBID_REUSE选项来断开连接(如果需要)。

    在日志中可以观察到打开/重用/关闭连接的整个过程:

    *   Trying 23.22.14.18...
    * Connected to httpbin.org (23.22.14.18) port 443 (#0)
    * ALPN, offering http/1.1
    * Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
    * successfully set certificate verify locations:
    *   CAfile: /etc/ssl/certs/ca-certificates.crt
      CApath: /etc/ssl/certs
    * NPN, negotiated HTTP1.1
    * SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
    * ALPN, server did not agree to a protocol
    * Server certificate:
    *    subject: OU=Domain Control Validated; OU=EssentialSSL Wildcard; CN=*.httpbin.org
    *    start date: Jan 12 00:00:00 2016 GMT
    *    expire date: Jan 19 23:59:59 2017 GMT
    *    subjectAltName: httpbin.org matched
    *    issuer: C=GB; ST=Greater Manchester; L=Salford; O=COMODO CA Limited; CN=COMODO RSA Domain Validation Secure Server CA
    *    SSL certificate verify ok.
    > GET /get HTTP/1.1
    Host: httpbin.org
    Accept: */*
    
    {
      "args": {}, 
      "headers": {
        "Accept": "*/*", 
        "Host": "httpbin.org"
      }, 
      "origin": "xx.xx.xx.xx", 
      "url": "https://httpbin.org/get"
    }
    < HTTP/1.1 200 OK
    < Server: nginx
    < Date: Tue, 04 Oct 2016 19:46:13 GMT
    < Content-Type: application/json
    < Content-Length: 154
    < Connection: keep-alive
    < Access-Control-Allow-Origin: *
    < Access-Control-Allow-Credentials: true
    < 
    * Connection #0 to host httpbin.org left intact
    * Found bundle for host httpbin.org: 0x95eac28 [can pipeline]
    * Re-using existing connection! (#0) with host httpbin.org
    * Connected to httpbin.org (23.22.14.18) port 443 (#0)
    > GET /ip HTTP/1.1
    Host: httpbin.org
    Accept: */*
    
    {
      "origin": "xx.xx.xx.xx"
    }
    < HTTP/1.1 200 OK
    < Server: nginx
    < Date: Tue, 04 Oct 2016 19:46:13 GMT
    < Content-Type: application/json
    < Content-Length: 33
    < Connection: keep-alive
    < Access-Control-Allow-Origin: *
    < Access-Control-Allow-Credentials: true
    < 
    * Closing connection 0
    

    执行第一个请求后curl,报告连接保持打开状态:

    Connection #0 to host httpbin.org left intact
    

    在执行第二个请求之前,它找到它并愉快地重用它:

    Found bundle for host httpbin.org: 0x95eac28 [can pipeline]
    Re-using existing connection! (#0) with host httpbin.org
    

    但是在第二次请求之后,由于在第二次请求之前的例子中有禁止重用连接,所以关闭了:

    Closing connection 0
    
    • 2
  2. Mike V.
    2020-01-30T21:24:00Z2020-01-30T21:24:00Z

    在套接字上:

    require("socket")
    require("ssl")
    -----
    
    local params = {
       mode = "client",
       protocol = "tlsv1",
       verify = "none",
       options = { "all", "no_sslv2", "no_sslv3", "no_ticket", "no_compression" } 
    }
    -----------------------------
    
    local url = 'https://httpbin.org/get'
    
    local host, file = url:match("://(.+)(/.+)")
    local headers = "HTTP/1.1\nHost: "..host.."\nConnection: keep-alive\n\n"
    local conn = socket.tcp()
    conn:connect(host, 443)
    
    conn = ssl.wrap(conn, params)
    conn:dohandshake()
    conn:send( "GET ".. file.." "..headers )
    --
        local  len, s, status = 0
        local str, answer = "",""
        while true do
              s, status = conn:receive('*l') 
              if s==nil or status =="closed" then   break end
              str=str..s..'\n'
              if s:find("Content%-Length")  then  len = s:match("Content%-Length:%s?(%d+)")   end
              if s=="" then 
                  answer, status = conn:receive(len)      break 
              end
        end
        print(str)
        print(answer)   
    
    conn:close()
    
    • 0

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何停止编写糟糕的代码?

    • 3 个回答
  • Marko Smith

    onCreateView 方法重构

    • 1 个回答
  • Marko Smith

    通用还是非通用

    • 2 个回答
  • Marko Smith

    如何访问 jQuery 中的列

    • 1 个回答
  • Marko Smith

    *.tga 文件的组重命名(3620 个)

    • 1 个回答
  • Marko Smith

    内存分配列表C#

    • 1 个回答
  • Marko Smith

    常规赛适度贪婪

    • 1 个回答
  • Marko Smith

    如何制作自己的自动完成/自动更正?

    • 1 个回答
  • Marko Smith

    选择斐波那契数列

    • 2 个回答
  • Marko Smith

    所有 API 版本中的通用权限代码

    • 2 个回答
  • Martin Hope
    jfs *(星号)和 ** 双星号在 Python 中是什么意思? 2020-11-23 05:07:40 +0000 UTC
  • Martin Hope
    hwak 哪个孩子调用了父母的静态方法?还是不可能完成的任务? 2020-11-18 16:30:55 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    Arch ArrayList 与 LinkedList 的区别? 2020-09-20 02:42:49 +0000 UTC
  • Martin Hope
    iluxa1810 哪个更正确使用:if () 或 try-catch? 2020-08-23 18:56:13 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5