Size: 2212
Comment:
|
Size: 3151
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 84: | Line 84: |
== Sample web server == {{{ # from https://twistedmatrix.com/trac/ from twisted.web import server, resource from twisted.internet import reactor, endpoints class Counter(resource.Resource): isLeaf = True numberRequests = 0 def render_GET(self, request): self.numberRequests += 1 request.setHeader(b"content-type", b"text/plain") content = u"I am request #{}\n".format(self.numberRequests) return content.encode("ascii") endpoints.serverFromString(reactor, "tcp:8080").listen(server.Site(Counter())) reactor.run() }}} * apt-get instal apache2-utils * apt-get install nginx * apt-get install links === /etc/nginx/conf.d/load-balancer.conf === {{{ upstream backend { server 172.28.128.4:8080; server 172.28.128.3:8080; server 172.28.128.6:8080; } server { listen 8080; location / { proxy_pass http://backend; } } }}} |
Twisted
Twisted is an event-driven networking engine written in Python and licensed under the open source MIT license.
Install from source
- su
- cd /tmp
wget http://twistedmatrix.com/Releases/Twisted/13.1/Twisted-13.1.0.tar.bz2
- tar xvif Twisted-13.1.0.tar.bz2
- cd Twisted-13.1.0
- python2.7 setup.py build
- python2.7 setup.py install
Test installation
- python
- from twisted.internet import protocol, reactor
- quit()
Slackbuild
- su
- cd /tmp
wget http://slackbuilds.org/slackbuilds/14.1/python/python-twisted.tar.gz
- tar xvzf python-twisted.tar.gz
- cd python-twisted
wget https://pypi.python.org/packages/source/T/Twisted/Twisted-13.2.0.tar.bz2
./python-twisted.SlackBuild
- installpkg /tmp/python-twisted-13.2.0-x86_64-1_SBo.tgz
Slackware64 14: python-twisted-13.2.0-x86_64-1_SBo.tgz
Echo server
1 from twisted.internet import protocol, reactor
2
3 class Echo(protocol.Protocol):
4 def dataReceived(self, data):
5 self.transport.write(data)
6
7 def connectionMade(self):
8 print('Connection made')
9
10 def connectionLost(self,reason):
11 print('Connection lost')
12 class EchoFactory(protocol.Factory):
13 def buildProtocol(self, addr):
14 return Echo()
15
16 if __name__=='__main__':
17 reactor.listenTCP(1234, EchoFactory())
18 reactor.run()
Echo client
1 import threading
2 import time
3 import socket
4
5 class Client (threading.Thread):
6 def __init__(self):
7 threading.Thread.__init__(self) #required
8
9 def run(self):
10 for x in range(1,100):
11 HOST = 'localhost'
12 PORT = 1234
13 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
14 print('connecting...')
15 s.connect((HOST, PORT))
16 print('sending config...')
17 s.send('test')
18 s.close()
19 print('complete')
20
21 if __name__=='__main__':
22
23 clients=[]
24 for x in range(1,100):
25 clients.append( Client() )
26
27 for c in clients:
28 c.start()
Sample web server
# from https://twistedmatrix.com/trac/ from twisted.web import server, resource from twisted.internet import reactor, endpoints class Counter(resource.Resource): isLeaf = True numberRequests = 0 def render_GET(self, request): self.numberRequests += 1 request.setHeader(b"content-type", b"text/plain") content = u"I am request #{}\n".format(self.numberRequests) return content.encode("ascii") endpoints.serverFromString(reactor, "tcp:8080").listen(server.Site(Counter())) reactor.run()
- apt-get instal apache2-utils
- apt-get install nginx
- apt-get install links
/etc/nginx/conf.d/load-balancer.conf
upstream backend { server 172.28.128.4:8080; server 172.28.128.3:8080; server 172.28.128.6:8080; } server { listen 8080; location / { proxy_pass http://backend; } }