【问题标题】:execute http post with Lua使用 Lua 执行 http post
【发布时间】:2013-08-06 21:32:03
【问题描述】:

我编写了一个 Python 脚本,它将数据 POST 到 apache 网络服务器,并且数据很好地到达了 $_POST 和 $_FILES 数组。现在我想在 Lua 中实现同样的东西,但我还不能实现。

我的 Python 代码如下所示:

    try:
        wakeup()
        socket.setdefaulttimeout(TIMEOUT)
        opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
        host = HOST
        func = "post_img"
        url = "http://{0}{1}?f={2}&nodemac={3}&time={4}".format(host, URI, func, nodemac, timestamp)
        if os.path.isfile(filename):
            data = {"data":open(filename,"rb")}
            print "POST time "+str(time.time())
            response = opener.open(url, data, timeout=TIMEOUT)
            retval = response.read()
            if "SUCCESS" in retval:
                return 0
            else:
                print "RETVAL: "+retval
                return 99
    except Exception as e:
        print "EXCEPTION time "+str(time.time())+" - "+str(e)
        return 99

到目前为止我想出的 Lua 代码:

#! /usr/bin/lua

http = require("socket.http")
ltn12 = require("ltn12")

http.request{
    url = "localhost/test.php?test=SEMIOS",
    method = "POST",
    headers = {
        ["Content-Type"] =  "multipart/form-data; boundary=127.0.1.1.1000.17560.1375897994.242.1",
        ["Content-Length"] = 7333
    },
    source = ltn12.source.file(io.open("test.gif")),
    sink = ltn12.sink.table(response_body)
}
print(response_body[1]) --response to request

但这段代码在执行时一直让我这样做:

$ ./post.lua
/usr/bin/lua: ./post.lua:17: attempt to index global 'response_body' (a nil value)
stack traceback:
        ./post.lua:17: in main chunk
        [C]: ?
reg@DesktopOffice:~$ 

【问题讨论】:

  • 我很困惑,您是在尝试编写与 Python 脚本相同的客户端 Lua 脚本,还是在尝试编写服务器端 Lua 脚本来处理您发送的数据Python 脚本?
  • @Paul,我需要编写一个与 Python 脚本相同的 Lua 脚本,是的。

标签: python http post lua http-headers


【解决方案1】:

有几个使用 Lua 发送 POST 数据的示例:from the author of luasocketSOThis example 直接处理文件,这与您使用的非常接近。

您对此问题的描述与comment you provided 不符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-02
    • 2020-02-09
    • 2021-12-18
    • 2012-08-13
    • 1970-01-01
    相关资源
    最近更新 更多