13 เมษายน 2560

Published เมษายน 13, 2560 by with 0 comment

Twisted - โมดูลที่ Network Programming คู่ควร

Twisted - โมดูลที่ Network Programming คู่ควร Twisted เป็น Framework สำหรับการเขียนโปรแกรมเครือข่ายเชิงเหตุการณ์ (event-driven network programming)  ที่เขียนในภาษา Python โดยมี protocols รองรับมากมาย ครบเครื่องสำหรับ Network Programming

  • มีจุดเด่นที่รองรับ protocols จำนวนมากมายในตัว ไม่ต้องใช้โมดูลอื่น

  • ใช้ MIT License

  • สนับสนุน TCP, UDP, SSL/TLS, IP multicast และ protocols จำนวนมาก เช่น HTTP, XMPP, NNTP, IMAP, SSH, IRC, FTP และอื่น

  • สามารถเป็น Server ได้

  • รองรับทั้ง Python 2 และ Python 3


ติดตั้งได้ด้วยคำสั่ง pip install Twisted

สามารถทำ Web Server แบบง่าย ๆ ตามตัวอย่าง

[python]
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()
[/python]

เสร็จแล้วเข้าไปที่ http://localhost:8080
ลองรันได้ที่ http://code.runnable.com/Up56G8zzSUc8AAR2/twisted-http-server-for-python

อ่านเอกสารได้ที่ twistedmatrix.com และ jcalderone.livejournal.com

0 ความคิดเห็น:

แสดงความคิดเห็น

แสดงความคิดเห็นได้ครับ :)