Install python, nodejs, and zeromq on Ubuntu server 12.10
Nodejs is a server programming framework based on Google V8 + JavaScript. For cloud computing, It is very suitable to use NJ for system management of web sites. The reason is:
A. It is simple enough.
B. fast enough.
C. Small enough.
D. Consistency between the frontend and backend.
The installation steps are as follows:
1. First, ensure that python, GCC, and g ++ are installed. If not, install:
$ Sudo apt-Get install Python
$ Sudo apt-Get install build-essential
$ Sudo apt-Get install gcc
$ Sudo apt-Get install g ++
2. Install Python bindings
2.1 install Python Automatic Installation Tool: easy_install
$ Sudo apt-Get install Python-setuptools
2.2 install the python compiling environment
$ Sudo apt-Get install Python-Dev
2.3 installing the binding of pythong zmq:
$ Sudo easy_install pyzmq
3 download the latest version of the source code package: node-v0.8.14.tar.gz
3.1 unzip:
$ Sudo tar-zxf node-v0.8.14.tar.gz
$ Node-v0.8.14 CD
3.2 installation by default:
$./Configure
$ Make
$ Sudo make install
3.3 select a directory for installation:
$./Configure -- prefix =/opt/node
$ Make-J 3 #3 = number of CPU cores + 1
$ Sudo make install
After the installation is complete, run the following command to check the installed version:
$ Node -- version
V0.8.14
3.4 next, refer to the following article to start your nodejs programming Journey:
Http://www.nodebeginner.org/index-zh-cn.html#a-full-blown-web-application-with-nodejs
4 zeromq
Zmq is a C-based message queue programming framework. Zeromq is useful in many ways and has the following features:
A. It is simple enough.
B. fast enough.
C. Small enough.
Below are the installation steps:
4.1 download: Zero-3.2.1
Http://www.zeromq.org/
$ Tar-xzf zeromq-3.2.1.tar.gz
$./Configure
$ Make
$ Sudo make install
$ Sudo ldconfig
4.2 write a client named mqclient. C and a server named mqserver. C for testing:
//// mqclient.c// Hello World client// Connects REQ socket to tcp://localhost:5555// Sends "Hello" to server, expects "World" back//#include <zmq.h>#include <string.h>#include <stdio.h>#include <unistd.h>int main (void){ void *context = zmq_ctx_new (); // Socket to talk to server printf ("Connecting to hello world server…\n"); void *requester = zmq_socket (context, ZMQ_REQ); zmq_connect (requester, "tcp://localhost:5555"); int request_nbr; for (request_nbr = 0; request_nbr != 10; request_nbr++) { zmq_msg_t request; zmq_msg_init_data (&request, "Hello", 6, 0, 0); printf ("Sending Hello %d…\n", request_nbr); zmq_msg_send (&request, requester, 0); zmq_msg_close (&request); printf ("prepare recv message\n"); zmq_msg_t reply; zmq_msg_init (&reply); if (-1==zmq_msg_recv (&reply, requester, 0)) { printf("recv data error.\n"); } printf ("Received World %d\n", request_nbr); zmq_msg_close (&reply); } zmq_close (requester); zmq_ctx_destroy (context); return 0;}
//// mqserver.c// Hello World server// Binds REP socket to tcp://*:5555// Expects "Hello" from client, replies with "World"//#include <zmq.h>#include <stdio.h>#include <unistd.h>#include <string.h>int main (void){ int ret; char buf[5]="world"; buf[5] = 0; void *context = zmq_ctx_new (); // Socket to talk to clients void *responder = zmq_socket (context, ZMQ_REP); ret = zmq_bind (responder, "tcp://*:5555"); if (ret==0) { printf("zmq_bind success\n"); } else { printf("zmq_bind error=%d:%s\n", ret, strerror(errno)); exit(ret); } while (1==1) { // Wait for next request from client printf("wait for clients...\n"); zmq_msg_t request; zmq_msg_init (&request); ret = zmq_msg_recv (&request, responder, 0); printf ("Received=%d\n", ret); zmq_msg_close (&request); // Do some 'work' sleep (1); // Send reply back to client zmq_msg_t reply; zmq_msg_init_data (&reply, buf, 6, 0, 0); ret = zmq_msg_send (&reply, responder, 0); printf("zmq_msg_send ret=%d\n", ret); zmq_msg_close (&reply); } // We never get here but if we did, this would be how we end zmq_close (responder); zmq_ctx_destroy (context); return 0;}
The following is a compilation command:
$ GCC mqclient. C-o mqclnt-lzmq
$ GCC mqserver. C-o mqsrvr-lzmq
Run:
$./Mqsrvr &
$./Mqclnt
5. Use python to compile zeromq programs
Reference: http://zguide.zeromq.org/py:all
This is a publishing (pub) and subscription (sub) model. The publisher publishes information, and the subscriber receives and processes and relevant information. A publisher corresponds to multiple subscribers.
| -----> Sub
Pub ------> | -----> sub B
| -----> Sub C
5.1 Publisher: mqpub. py
############################################################## mqpub.py# Weather update server# Binds PUB socket to tcp://*:5556# Publishes random weather updates#############################################################import zmqimport randomcontext = zmq.Context()socket = context.socket(zmq.PUB)socket.bind("tcp://*:5556")while True: zipcode = random.randrange(1,100000) temperature = random.randrange(1,215) - 80 relhumidity = random.randrange(1,50) + 10socket.send("%d %d %d" % (zipcode, temperature, relhumidity))
5.2 subscriber: mqsub. py
############################################################## mqsub.py# Weather update client# Connects SUB socket to tcp://localhost:5556# Collects weather updates and finds avg temp in zipcode#############################################################import sysimport zmq# Socket to talk to servercontext = zmq.Context()socket = context.socket(zmq.SUB)print "Collecting updates from weather server"socket.connect ("tcp://localhost:5556")# Subscribe to zipcode, default is NYC, 10001zip_filter = sys.argv[1] if len(sys.argv) > 1 else "10001"socket.setsockopt(zmq.SUBSCRIBE, zip_filter)# Process 5 updatestotal_temp = 0for update_nbr in range (5): string = socket.recv() zipcode, temperature, relhumidity = string.split() total_temp += int(temperature)print "Average temperature for zipcode '%s' was %dF" % (zip_filter, total_temp / update_nbr)
5.3 open two terminals and run them separately:
$ Python./mqpub. py
$ Python./mqsub. py