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, ['n2@uwiger-laptop','n1@uwiger-laptop']}]},
{start_dist_ac, true},
{sync_nodes_optional, ['n2@uwiger-laptop','n1@uwiger-laptop']},
{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).

转载于:https://www.cnblogs.com/huangxiaoyi/p/7182614.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值