2013-10-03 28 views
1

我使用從扭曲教程下面的腳本(略有修改):扭曲蟒系統守護進程和端口綁定

from twisted.application import internet, service 
from twisted.internet import reactor, protocol, defer 
from twisted.protocols import basic 
from twisted.web import client 

class FingerProtocol(basic.LineReceiver): 

    def lineReceived(self, user): 
     d = self.factory.getUser(user) 

     def onError(err): 
      return "Internal server error" 
     d.addErrback(onError) 

     def writeResponse(message): 
      self.transport.write(message + "\r\n") 
      self.transport.loseConnection() 
     d.addCallback(writeResponse) 

class FingerFactory(protocol.ServerFactory): 
    protocol = FingerProtocol 

    def __init__(self, prefix): 
     self.prefix = prefix 

    def getUser(self, user): 
     return client.getPage(self.prefix + user) 

application = service.Application('finger', uid=1, gid=1) 
factory = FingerFactory(prefix="http://livejournal.com/~") 
internet.TCPServer(7979, factory).setServiceParent(
    service.IServiceCollection(application)) 

我保存爲finger_daemon.tac並用

twistd -y finger_daemon.tac \ 
    -l /home/me/twisted/finger.log \ 
    --pidfile=/home/me/twisted/finger.pid 

當然但是運行它不會綁定到79,因爲它是一個特權端口。我也嘗試與sudo一起運行,沒有區別。

然後我試圖改變TCPServer端口7979,然後連接到後臺程序一旦與

telnet 127.0.0.1 7979 

運行,我得到Connection Refused錯誤。這裏具體是怎麼回事?守護進程如何在Twisted中工作?

回答

1

當我運行這段代碼,我看到以下日誌消息:

2013-10-02 23:50:34-0700 [-] failed to set uid/gid 1/1 (are you root?) -- exiting. 

,然後twistd退出。所以你需要做sudo twistd,然後增加了一堆python路徑管理問題...

你爲什麼要設置uidgid參數?您正試圖將其作爲daemon用戶運行?爲了守護進程你不需要這麼做。只要刪除uid=1, gid=1參數到Application就可以爲我工作。

+0

真棒,做伎倆!想知道爲什麼這是他們的官方教程... – lollercoaster

+0

也很重要 - 你怎麼看錯誤? twistd默默爲我運行 – lollercoaster

+1

我在Twisted上工作,所以直接鏈接到提到這個例子的教程會很有用:-)。 – Glyph