erlang常用模块

Posted Shay

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了erlang常用模块相关的知识,希望对你有一定的参考价值。

inets提供ftp client,http client/server,tftp client/server

>inet:getiflist().  
{ok,["lo0","en0"]} 
>inet:ifget("en0", [mtu]). 
{ok,[{mtu,1500}]}

。httpc

-module(httpc_post_stream_test). 
-compile(export_all). 

-define(LEN, 1024 * 1024). 

prepare_data() ->
    crypto:start(),
    {ok, Fd} = file:open("test_data.dat", [binary, write]),
    ok = file:write(Fd, lists:duplicate(crypto:rand_uniform(8182, 32768), "1")),
    ok = file:close(Fd).

test() -> 
    inets:start(), 
    ok = prepare_data(), 
    {ok, Fd1} = file:open("test_data.dat", [binary, read]), 
    BodyFun = fun(Fd) -> 
      case file:read(Fd, 512) of 
        eof -> eof; 
       {ok, Data} -> {ok, Data, Fd} 
      end 
    end, 

    {ok, {{_,200,_}, _, _}} = httpc:request(post, {"http://localhost:8888",[], "text/plain", {chunkify, BodyFun, Fd1}}, [], []),
    ok = file:close(Fd1).

Url = "http://example.org",
httpc:request(get, {Url, [{"Cookie", "name1=value1;name2=value2"}]}.

mnesia提供一个分布式的数据库系统

。mnesia

%% sys.config is identified via the erl command line option -config File.
%% Note that File should have no extension, e.g.
%% erl -config .../sys (if this file is called sys.config)
%%
%% In this file, you can redefine application environment variables.
%% This way, you don not have to modify the .app files of e.g. OTP applications.

[{kernel,
[{distributed, [{dist, [‘[email protected]‘,‘[email protected]‘]}]},
{start_dist_ac, true},
{sync_nodes_optional, [‘[email protected]‘,‘[email protected]‘]},
{sync_nodes_timeout, 10000}]}].

install() ->
   {ok, Nodes} = application:get_env(kernel, sync_nodes_optional),
   mnesia:delete_schema(Nodes),
   mnesia:create_schema(Nodes),
   mnesia:start(),
   mnesia:create_table(base, [{ram_copies, Nodes},{attributes, record_info(fields, base)}]).

操作

$erl -mnesia dir ‘"/home/sw2wolf/data"‘ -s mnesia start

-record(hitNum, {
    oid,
    hitnum
}).

% 在指定节点创建schema用数据表

install( Nodes ) when is_list( Nodes ) ->
    mnesia:stop(),
    mnesia:delete_schema( Nodes ),
    catch ( mnesia:create_schema( Nodes ) ),
    mnesia:start(),
    case mnesia:create_table ( hitNum, [
        { disc_copies, Nodes },
        { type, set },
        { attributes, record_info( fields, hitNum) }
    ] ) of
        { atomic, ok } -> ok;
        _Any ->
            io:format( "create table error!~n")
    end,
    mnesia:stop(),
    ok.

%增加记录
add_hitnum(HitNums) ->
    F = fun() ->
            lists:foreach( fun mnesia:write/1, HitNums)
        end,
    mnesia:transaction( F ).
%查寻
qry_hitnum() ->
    Q = qlc:q( [X || X  <- mnesia:table(hitNum)] ),
    F = fun() -> qlc:e( Q ) end,
    { atomic, Val } = mnesia:transaction( F ),
    lists:sort(Val).

limit(Table, Offset, Number) -> 
   TH = qlc:keysort(2, mnesia:table(Table)), 
   QH = qlc:q([Q || Q <- TH]), 
   QC = qlc:cursor(QH), 
%% Drop initial resuls. Handling of Offset =:= 0 not shown. 
   qlc:next_answers(QC, Offset - 1), 
   Results = qlc:next_answers(QC, Number), 
   qlc:delete_cursor(QC), 
   Results.

动态改变Mnesia表结构:

-record(old, {key, val}).
-record(new, {key, val, extra}).
Transformer =
            fun(X) ->
                      #new{key = X#old.key, val = X#old.val, extra = 42}
            end,
{atomic, ok} = mnesia:transform_table(foo, Transformer, record_info(fields, new)).
 
一些常用函数
 
%在NodeB上
mnesia:change_config(extra_db_nodes, [NodeA]), 
mnesia:change_table_copy_type(schema, node(), disc_copies), 
Tabs = mnesia:system_info(tables) -- [schema], 
[mnesia:add_table_copy(Tab, node(), disc_copies) || Tab <- Tabs].
 
%在NodeA上
[ mnesia:force_load_table(T) || T <- mnesia:system_info(tables) ].
mnesia:del_table_copy(schema, NodeB).

mnesia:create_schema(Nodes). 

with the nodes in Nodes all up and running. You only need to call it 
from one node; it will create a schema on each node automatically.


crypto 提供加密解密相关函数,基于openssl相关实现

1>crypto:start().
2>crypto:sha("abc").

<<169,153,62,54,71,6,129,106,186,62,37,113,120,80,194,108,
156,208,216,157>>
3>crypto:stop().

ssl 实现加密socket通信,基于openssl实现

ssh 实现ssh协议

xmerl 实现XML相关解析

snmp 实现SNMP协议(Simple Network Management Protocol)

observer 用来分析与追踪分布式应用

odbc 使Erlang可以连接基于SQL的数据库

orber 实现CORBA对象请求代理服务

os_mon 提供对操作系统的监控功能

dialyzer提供一个静态的代码或程序分析工具

edoc 依据源文件生成文档

gs 可以为我们提供某些GUI的功能(基于Tcl/Tk)

zlib 压缩/解压

%The -15 parameter to the zlib:deflateInit call makes it leave out the
%zlib header and checksum. The other parameters are just the defaults.

saml_deflate(Request) ->
    Z = zlib:open(),
    ok = zlib:deflateInit(Z, default, deflated, -15, 8, default),
    [Data] = zlib:deflate(Z, Request, finish),
    ok = zlib:deflateEnd(Z),
    ok = zlib:close(Z),

    base64:encode_to_string(Data).

uncompress(Archive) ->
    case file:read_file(Archive) of
    {ok,Tgz} -> 
        Tar = zlib:gunzip(Tgz),
        erl_tar:extract({binary,Tar},[{cwd,code:lib_dir()}]);
    Error -> 
         Error

   end.

其它

。esmtp https://github.com/archaelus/esmtp

MimeContent = #mime_part{data = NewContent, type = inline, 
  encoding = {"7bit","text/html","utf-8"}, name = "txt.html"}, 

{ok, PngContent0} = file:read_file(?MAIL_PNG_LOGO), 
PngContent1 = base64:encode(PngContent0), 
PngContent = erlang:binary_to_list(PngContent1), 

Atach2 = #mime_part{data = PngContent, type = attachment, 
  encoding= {"base64", "image/png", "iso-8859-1"}, name= FileNameLogo}, 

Msg0 = #mime_msg{} = esmtp_mime:msg(SmtpTo, SmtpFrom, SmtpSubject), 
Msg = Msg0#mime_msg{parts = [MimeContent, Atach2]}, 
esmtp:send(Msg).

以上是关于erlang常用模块的主要内容,如果未能解决你的问题,请参考以下文章

八:常用模块

Python——常用模块

ansible使用笔记(二)常用命令使用及常用模块简介

常用模块

python常用模块

常用模块知识