Python
Thursday, December 5, 2013
Tuesday, December 3, 2013
Asynchronous Programming and Twisted
What is Twisted?
Twisted is a event driven network framework, it is written in python and is FOSS(Free and Open Source Software) under MIT Licence.
An Introduction to Asynchronous Programming and Twisted
Synchronous programming
The simplest style of programming. Each task is perfomed one at a time, with one finishing completely before another is started. And if the tasks are always performed in a definite order, the implementation of a later task can assume that all earlier tasks have finished without errors, with all their output available for use — a definite simplification in logic. In this model, each task is performed in a separate thread of control. The threads are managed by the operating system and may, on a system with multiple processors or multiple cores, run truly concurrently, or may be interleaved together on a single processor. The point is, in the threaded model the details of execution are handled by the OS and the programmer simply thinks in terms of independent instruction streams which may run simultaneously. Although the diagram is simple, in practice threaded programs can be quite complex because of the need for threads to coordinate with one another. Thread communication and coordination is an advanced programming topic and can be difficult to get right.
In this model, each task is performed in a separate thread of control. The threads are managed by the operating system and may, on a system with multiple processors or multiple cores, run truly concurrently, or may be interleaved together on a single processor. The point is, in the threaded model the details of execution are handled by the OS and the programmer simply thinks in terms of independent instruction streams which may run simultaneously. Although the diagram is simple, in practice threaded programs can be quite complex because of the need for threads to coordinate with one another. Thread communication and coordination is an advanced programming topic and can be difficult to get right.
Now start writing some code to understand better
from twisted.internet import reactor, protocol, endpoints
class UpperProtocol(protocol.Protocol):
def connectionMade(self):
self.transport.write("Hi its my first twisted code")
def connectionLost(self,reason):
pass
def dataReceived(self, data):
self.transport.write(data.upper())
self.transport.loseConnection()
factory=protocol.ServerFactory()
factory.protocol = UpperProtocol
endpoints.serverFromString(reactor, "tcp:8080").listen(factory)
reactor.listenTCP(8080,factory)
reactor.run()
this is the server side code for twisted, which is running on port number 8080 of the localhost.
client side using twisted can be written as follows
from twisted.internet import reactor, protocol, endpoints
class UppercaseClientProtocol(protocol.Protocol):
def connectionMade(self):
self.transport.write(self.factory.text)
self.transport.write("\r \n")
def dataReceived(self, data):
print data
if __name__ == '__main__':
import sys
assert ':' in sys.argv[1], "need host:port for argument 1"
data_to_send = sys.argv[2:]
endpoint = endpoints.clientFromString(reactor, "tcp:"+sys.argv[1])
for data in data_to_send:
print "sending", data
factory = protocol.ClientFactory()
factory.protocol = UppercaseClientProtocol
factory.text = data
endpoint.connect(factory)
reactor.run()
Monday, November 25, 2013
First Python Script
Today I am writing about python basics. Python is a high level, scripting language. Linux terminal is used as a interpreter for python. Python is an object oriented programming language, it use very few punctuation for construction of syntax but follows indentation very strictly.
write basic python program:
#!/usr/bin/python
def main():
print "Hello World!!!!!"
if __name__=="__main__":
main()
$python Hello_World.py

