2016-04-14 10 views
0

SMB接続を監視したいのですが、以下のコードを作成しましたが、ネットワークを繋ぐのが心配です。SMBソケットの接続を繰り返してもいいですか

このように接続を繰り返し開いたり閉じたりしても問題ありませんか?

import socket 
import time 
import threading 

class SMBConnectionMonitor(threading.Thread): 
    def __init__(self, host, poll_period=60, timeout=5): 
     super(SMBConnectionMonitor, self).__init__() 

     self.host = host 
     self.poll_period = poll_period 
     self.timeout = timeout 
     self.connected = False 
     self.stop_requested = False 

    def stop(self): 
     self.stop_requested = True 

    def run(self): 
     while not self.stop_requested: 
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
      sock.settimeout(self.timeout) 
      try: 
       sock.connect((self.host, 445)) 
       # successful, if this is the first time update the status 
       if not self.connected: 
        self.connected = True 
      except socket.error as e: 
       # can't connect, if this is first time update the status 
       if self.connected: 
        self.connected = False 
      sock.close() 

      # wait for the poll period before trying another connection 
      for i in range(self.poll_period): 
       if self.stop_requested: return 
       time.sleep(1) 

monitor = SMBConnectionMonitor("remote-computer", poll_period=10) 
monitor.start() 
monitor.join(timeout=30) 
monitor.stop() 

答えて

1

このような接続を1秒に1回開いたり閉じたりすると、ダイヤルアップでもトラフィックは無視できます。遅延をループから完全に取り除いたとしても、現代のネットワークにはごくわずかな、恐らく目立つことはないでしょう。比較的遅いPythonの速度とTCP接続の開始に伴う固有の遅れとを組み合わせることで、たとえ試しても、1つの接続を繰り返し開閉することでネットワークを縛ることができなくなります。

関連する問題