On Thu, Feb 3, 2011 at 5:08 PM, Roberto De Ioris <[email protected]> wrote:
>
> Il giorno 03/feb/2011, alle ore 12.15, luigi scarso ha scritto:
>
>> On Thu, Feb 3, 2011 at 11:59 AM, Roberto De Ioris <[email protected]> wrote:
>>> yes, you can always use http (or even the uwsgi protocol) to erlang->uwsgi
>>> communication
>> hm,  I haven't found  an erlang client that understand uwsgi protocol
>> but I suspect that should be easy to build.
>> I will investigate, it could be a good exercise to understand uwsgi 
>> internals.
>>
>>
>
>
> http://projects.unbit.it/uwsgi/browser/contrib/uwsgi.erl
>
> this is very simple (and ugly)
>
>
> uwsgi:send(host, port, message)
>
> example:
>
> uwsgi:send("127.0.0.1", 3031, [ "SERVER_PROTOCOL", "HTTP/1.1", "SERVER_NAME", 
> "example.com", "REQUEST_METHOD, "GET"]).
It seems to work, but I need to  understand better  the interactions.



%% uwsgiecho.erl
-module(uwsgiecho).
-export([start/0, loop/0, echo/1]).
echo(Message) ->
        {Message}.
loop() ->
        receive
                stop  ->
                    stop();
                Message1 ->
                        io:format("received a message ~p ~n",[Message1]),
                        %% 'uwsginode@johnII' is the uWSGI erlang node
                        %% useless_atom should be the registered_name as in
                        %% {registered_name,node_name}
                        %% but it's not used by uwsgi
                        {useless_atom,'uwsginode@johnII'}  ! echo(Message1),
                        loop()
        end,
        end_loop.

start() ->
        register(echoer, spawn(uwsgiecho, loop, [])).

stop() ->
    stopped.





## erlang-echo.py
import uwsgi
def application(env, start_response):
    uwsgi_erlang = uwsgi.erlang_connect("uwsgierlang@johnII")
    start_response('200 OK', [('Content-Type', 'text/plain')])
    uwsgi.erlang_send_message(uwsgi_erlang, "echoer", "env="+str(type(env)))
    for j in uwsgi.erlang_recv_message(uwsgi_erlang):
        yield j + '\n'

    uwsgi.erlang_send_message(uwsgi_erlang, "echoer", "env="+str(len(env)))
    for j in uwsgi.erlang_recv_message(uwsgi_erlang):
        yield j + '\n'

    for k in env:
        uwsgi.erlang_send_message(uwsgi_erlang, "echoer", str(k)+ "="
+ str(env[k]) )
        for j in uwsgi.erlang_recv_message(uwsgi_erlang):
            yield j + '\n'

    #yield uwsgi.erlang_rpc(uwsgi_erlang, "uwsgitest", "hello", [])
    uwsgi.erlang_close(uwsgi_erlang)


$1> uwsgi -s 127.0.0.1:38079  --erlang uwsginode@johnII
--erlang-cookie cookie_dispatcher -p 2 -w erlang-echo
*** Starting uWSGI 0.9.6.7 (32bit) on [Thu Feb  3 17:57:20 2011] ***
compiled with version: 4.2.4 (Ubuntu 4.2.4-1ubuntu3)
Python version: 2.6.5 (r265:79063, Jan 19 2011, 17:44:48)  [GCC 4.2.4
(Ubuntu 4.2.4-1ubuntu3)]
uWSGI running as root, you can use --uid/--gid/--chroot options
 *** WARNING: you are running uWSGI without its master process manager ***
your memory page size is 4096 bytes
allocated 412 bytes (0 KB) for 1 request's buffer.
binding on TCP port: 38079
your server socket listen backlog is limited to 100 connections
initializing hooks...done.
Erlang C-Node initialized on port 54048 you can access it with name
uwsginode@johnII
...getting the applications list from the 'erlang-echo' module...
uwsgi.applications dictionary is not defined, trying with the
"applications" one...
applications dictionary is not defined, trying with the "application" callable.
application 0 () ready
setting default application to 0
spawned uWSGI worker 1 (pid: 1324)
Erlang mode enabled for worker 2.
spawned uWSGI worker 2 (pid: 1325)





$2>erl -sname uwsgierlang -setcookie cookie_dispatcher
1> uwsgiecho:start() .


$3>  curl http://127.0.0.1:38077/?FOO=baa
env=<type 'dict'>
env=24
wsgi.multiprocess=True
REQUEST_METHOD=GET
PATH_INFO=/
SERVER_PROTOCOL=HTTP/1.1
QUERY_STRING=FOO=baa
CONTENT_LENGTH=
HTTP_USER_AGENT=curl/7.18.0 (i486-pc-linux-gnu) libcurl/7.18.0
OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1
SERVER_NAME=localhost
REMOTE_ADDR=127.0.0.1
wsgi.url_scheme=http
SERVER_PORT=38077
DOCUMENT_ROOT=/home/nginx/html
wsgi.input=<open file 'wsgi_input', mode 'r' at 0xb7c09968>
HTTP_HOST=127.0.0.1:38077
wsgi.multithread=False
REQUEST_URI=/?FOO=baa
HTTP_ACCEPT=*/*
wsgi.version=(1, 0)
wsgi.run_once=False
wsgi.errors=<open file 'wsgi_input', mode 'w' at 0xb7c09910>
REMOTE_PORT=38744
uwsgi.version=0.9.6.7
CONTENT_TYPE=
wsgi.file_wrapper=<built-in function uwsgi_sendfile>

In the erlang shell
1>
received a message 'env=<type \'dict\'>'
received a message 'env=24'
received a message 'wsgi.multiprocess=True'
received a message 'REQUEST_METHOD=GET'
received a message 'PATH_INFO=/'
received a message 'SERVER_PROTOCOL=HTTP/1.1'
received a message 'QUERY_STRING=FOO=baa'
received a message 'CONTENT_LENGTH='
received a message 'HTTP_USER_AGENT=curl/7.18.0 (i486-pc-linux-gnu)
libcurl/7.18.0 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1'
received a message 'SERVER_NAME=localhost'
received a message 'REMOTE_ADDR=127.0.0.1'
received a message 'wsgi.url_scheme=http'
received a message 'SERVER_PORT=38077'
received a message 'DOCUMENT_ROOT=/home/nginx/html'
received a message 'wsgi.input=<open file \'wsgi_input\', mode \'r\'
at 0xb7c09910>'
received a message 'HTTP_HOST=127.0.0.1:38077'
received a message 'wsgi.multithread=False'
received a message 'REQUEST_URI=/?FOO=baa'
received a message 'HTTP_ACCEPT=*/*'
received a message 'wsgi.version=(1, 0)'
received a message 'wsgi.run_once=False'
received a message 'wsgi.errors=<open file \'wsgi_input\', mode \'w\'
at 0xb7c09968>'
received a message 'REMOTE_PORT=55389'
received a message 'uwsgi.version=0.9.6.7'
received a message 'CONTENT_TYPE='
received a message 'wsgi.file_wrapper=<built-in function uwsgi_sendfile>'

Test send in the erlang shell:
2> uwsgi:send("127.0.0.1", 38079, [ "SERVER_PROTOCOL", "HTTP/1.1",
"SERVER_NAME", "example.com", "REQUEST_METHOD", "GET"]), ok.
received a message 'env=<type \'dict\'>'
received a message 'env=12'
received a message 'wsgi.multiprocess=True'
received a message 'wsgi.version=(1, 0)'
received a message 'SERVER_NAME=example.com'
received a message 'wsgi.run_once=False'
received a message 'wsgi.errors=<open file \'wsgi_input\', mode \'w\'
at 0xb7c09910>'
received a message 'wsgi.multithread=False'
received a message 'wsgi.url_scheme=http'
received a message 'uwsgi.version=0.9.6.7'
received a message 'wsgi.input=<open file \'wsgi_input\', mode \'r\'
at 0xb7c09968>'
received a message 'REQUEST_METHOD=GET'
received a message 'SERVER_PROTOCOL=HTTP/1.1'
received a message 'wsgi.file_wrapper=<built-in function uwsgi_sendfile>'
ok

And in the uwsgi shell

[pid: 1324|app: 0|req: 5/5]  () {6 vars in 74 bytes} [Thu Feb  3
18:02:01 2011] GET  => generated 396 bytes in 3 msecs (HTTP/1.1 200) 1
headers in 45 bytes (0 async switches on async core 0)
-- 
luigi
_______________________________________________
uWSGI mailing list
[email protected]
http://lists.unbit.it/cgi-bin/mailman/listinfo/uwsgi

Reply via email to