Lua功能模块之“CURL”
作者:糖果
Curl是一个WEB开发常用的工具,直接用官网的翻译
curl是一个开源的命令行工具,也是一个库,用于传输URL语法的工具,支持DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMTP, SMTPS, Telnet and TFTP等。
在lua中,curl就是以库的形式存在的,安装过程比较简单:sudo luarocks install luacurl。
另外,curl还是支持代理的方式访问主机,这个很有用,之后会用一个模拟DDOS攻击程序说明他的用处。
这一次,我们用一个和SAE云平台相关的机能,说明pycurl的使用。
简单的说明一下,SAE云平台是国内较早的云开开放平台之一,经过多年的积累,有广大的用户基础,提供便利的开发平台,最近开放了一个实时LOG查询功能。用户可以通过其对外开放的REST API,查询自己运行在云平台上的APP产出的LOG。
文档说明:
接入流程概述:
1.计算取得安全签名 ...
《程序设计实践》-前言
Preface
Have you ever…
你是否曾经
wasted a lot of time coding the wrong algorithm?
浪费了大量的时间,写了一个错误的算法。
used a data structure that was much too complicated?
采用的数据接结构过于复杂?
tested a program but missed an obvious problem?
测试过的程序,却遗漏了明显的问题?
spent a day looking for a bug you should have found in five minutes?
五分钟解决的BUG,却消耗一天?
needed to make a program run three times faster and use less memory?
希望你的程序能事半功倍,快速而省内存?
struggled to move a program from a workstation to a PC or viceversa?
努力的将程序辗转切换于开发环境和生产环境之间?
t ...
如何创建部署WSGI类型的(Django, Tornado, Flask)Python应用
作者:糖果
第一部分:安装必要工具。
1.因为这是部署Python开发环境,所以安装pip可以简化一些软件的安装过程。(PIP对应Lua的luarocks)
sudo apt-get install python-pip
安装三个Python框架
sudo pip install flask
sudo pip install django==1.5.1
sudo pip install tornado==3.1.1
2.安装Gunicorn,这是运行Python的WSGI HTTP服务。
sudo pip install gunicorn
3.Virtualenv, 安装这个是因为,在部署Django的时候,使用了不同的版本。
sudo pip install virtualenv
第二部分:创建部署应用。
1.创建一个WSGI类型的Tornado应用。
import tornado.web
import tornado.wsgi
class MainHandler(tornado.web.RequestHandler):
d ...
Lua中脚本中加载C语言的.SO共享库
作者:糖果
在Lua中,可以使用loadlib的方式直接的加载C语言写的库,如同加载.lua文件一样。C写的模块可以做一些对效率要求相对比较高的模块,或是一些底层操作。下面举例
说明:
第一步:创建C模块文件。
foo.h头文件
#ifndef foo_h__
#define foot_h__
extern void foo(lua_State* L);
#endif
foo.c实现文件
#include <stdio.h>
#include "lauxlib.h"
void foo(lua_State* L)
{
puts("Hello, I'm a shared library");
}
第二步:创建.o文件。
gcc -c -Wall -Werror -fpic foo.c -I/usr/include/lua5.1
注意一下的是.h文件中包含了"lauxlib.h"文件,所以要在编译的时候加上-I选项,后面追加.h文件的路径。
第三步:创建.so文件。
gcc -shared -o l ...
《Lua游戏AI开发指南 --- C/C++中调用Lua函数》
C/C++ calling Lua functions
The sandbox hooks into the Lua script through three predefined global Lua functions:
Sandbox_Initialize, Sandbox_Cleanup, and Sandbox_Update. When the sandbox is first
attached to the corresponding Lua script, the Sandbox_Initialize function is called. Each
update tick of the sandbox will also invoke the Sandbox_Update function in the Lua script.
When the sandbox is being destroyed or reloaded, the Sandbox_Cleanup function will
have an opportunity to perform any scrip ...
WEB IDE环境运行Lua网页应用
作者:糖果
如Python和Ruby一样,Lua也可以创建WEB应用,之前提过的一个Lua WEB框架
这次就在WEB IDE环境下,部署一下LuaWEB的运行环境,并创建一个Lua WEB应用。
第一步:安装WEB服务器, Openresty。
1).下载安装包
wget https://openresty.org/download/ngx_openresty-1.7.10.1.tar.gz
2).解压
tar xzvf ngx_openresty-1.7.10.1.tar.gz
3).安装依赖包
sudo apt-get install libreadline-dev
sudo apt-get install libncurses5-dev
sudo apt-get install libpcre3-dev
sudo apt-get install libssl-dev
sudo apt-get install perl
sudo apt-get install make
sudo apt-get install build-essentia ...
《Lua游戏AI开发指南 --- Lua调用C/C++函数》
作者:糖果
Exposing C++ functions to Lua takes place through a process called function binding. Any bound functions exposed to Lua become accessible either as a global function or as a function available through a package. Packages in Lua are similar to namespaces in C++ and are implemented as a global table within Lua.
通过暴漏C函数给Lua的过程,叫做函数绑定。任何绑定函数暴漏给Lua成为一个可访问的全局函数,或是作为可用的包函数。Lua中的Packages(包)类似于C中的命名空间,作为一个类似全局表被实现的。
Function binding
Any function exposed to Lua must fit the lua_CFunction declara ...
Openresty的Lua定时器(计划任务)
在系统中有一类需求是:周期性的执行某些任务,利用定时的timer去实现这种操作。
Openresty为Lua提供了这种机制实现的API,通过设定timer来完成这种类似计划任务功能。
下面,就是一个典型的Openresty的timer API的使用例子:
实现思路是,通过一个timer设定调用一个函数,在函数内部还有一个循环递归的timer调用,调用函数自身,实现周期性的函数执行。
–此函数的主要的目的是6秒钟的时间,对redis中某Key,进行数值累加。
local handler
function handler(premature, params)
--定时执行一个redis的累加计数操作。
RedisCommon.add()
--递归的timer,重复调用handler函数。
local ok, err = ngx.timer.at(6, handler, "params-data")
ngx.log(ngx.DEBUG, "ok:", ok, " err:", err)
end
–第一次设定timer,调用hander函数。
local ok, ...
LUA与STOMP协议
作者:糖果
STOMP协议是一种简单的消息文本协议。协义本身简单明了,用消息头定义和消息体数据传输。
RabbitMQ做为一种队列中间件,提供了STOMP协议的支持,我们可以通过STOMP协议向队列发送消息。下面的例子中,我们将使用LUA程序向RabbitMQ发送消息, 通过Python程序读取消息。
send.lua文件
local client = require "stomp"
local mq, err = client:new()
local ok, err = mq:connect("127.0.0.1", 61613)
local msg = "say hi!"
local headers = {}
headers["destination"] = "/queue/test"
headers["app-id"] = "APP"
local ok, err = mq:send(info_json, headers)
对上面的代码说明一下:
连接时候RabbitMQ的IP是本机的127.0.0.1, STOMP协议的服务的端口是默认的61613。
在hea ...
MoonScript脚本开发简介
Coffescript是一种中间的脚本,可以把这种脚本翻译成JavaScript。而MoonScript,是可以翻译成lua语言的中间脚本。
本文简单的介绍的:
如何在VIM中,实现MoonScript语法高亮。
如何简单的编译MoonScript脚本。
1.安装MoonScript
sudo luarocks install moonscript
2.创建.moon源文件
app.moon
lapis = require "lapis"
class extends lapis.Application
"/": =>
"Welcome to Lapis #{require "lapis.version"}!"
3.安装MoonScript语法高亮的插件。
3-1.下载vim的bundle插件管理程序。
git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle
3-2.创建.vimrc ...


