0

우분투 12.04 상자에 토런트가 uTorrent와 함께 다운로드 될 때 알림을 보내도록 작은 파이썬 스크립트를 만들었습니다. pgrep -l torrent를 실행하면 scipt가로드되고이를 죽일 수 없습니다. utorrent는 토렌트 이름을 전달하는 스크립트를 호출합니다. 스크립트는 잘 작동하고 터미널에서 실행할 때 종료되지만 utorrent가 호출 할 때 닫히지 않습니다. 스크립트의 맨 아래에 sys.exit()를 추가하려고 시도했지만 프로세스를 중지하지 않고 알림을 보내지 못하게했습니다.파이썬 uTorrent 알림 스크립트가 종료되지 않습니다

나는 scipt를 닫기 위해 할 수 있습니까? 또한 이러한 프로세스를 어떻게 죽일 수 있을지에 대한 아이디어가 있습니까? 나는 pkill과 htop을 죽이려고했다.

감사합니다.

#!/usr/bin/python 

import sys 
import os 
import socket 

def network_notify(message): 
    host = ['192.168.0.4', '192.168.0.6'] #server IP 
    port = 50000 
    size = 1024 
    title = 'Torrent Downloaded' 

    for i in host: 
     try: 
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
      s.connect((i,port)) 
     except: 
      None 
     else: 
      s.send('\"' + title + '\" \"' +message+'"') 
      data = s.recv(size) 
      s.close() 

if len(sys.argv) > 1: 
    name = ' '.join(sys.argv[1:]) 
    network_notify(name) 

을 heres pgrep을

[email protected]Netb$ pgrep -l torrent 
27516 torrentNotify.p 
27518 torrentNotify.p 
27520 torrentNotify.p 
27521 torrentNotify.p 
27522 torrentNotify.p 
27529 torrentNotify.p 
27531 torrentNotify.p 
27540 torrentNotify.p 
27541 torrentNotify.p 
27545 torrentNotify.p 
27546 torrentNotify.p 
27550 torrentNotify.p 
27551 torrentNotify.p 
27552 torrentNotify.p 
27553 torrentNotify.p 
27555 torrentNotify.p 
27558 torrentNotify.p 
27567 torrentNotify.p 
27570 torrentNotify.p 
27571 torrentNotify.p 
27573 torrentNotify.p 
27574 torrentNotify.p 
28959 torrentNotify.p 
28965 torrentNotify.p 
28970 torrentNotify.p 
+0

무엇이'wchan'입니까? –

+0

wchan 모두 출구입니다. – James

+1

catch-all 'except :'절이 유용한 정보를 숨길 수 있습니다. 또한,'socket' 메소드 호출 중 하나가 걸려있을 수 있습니다. 어떤 디버깅을 위해 어떤 로깅을 추가하는 것이 유용 할 수 있습니다. – martineau

답변

1

이 밖으로 시도하고 스크립트를 실행할 때 그것은 당신이 uTorrent 디버그 절약을 위해 /path/to/script.py >> debug.txt 할라고 할 때 확인하십시오. 이렇게하면 더 나은 오류 처리를 제공하고 완료되면 명시 적으로 소켓을 닫아야합니다. 또한 작은 제한 시간도 추가했습니다.

저는 소켓의 일부가 스크립트의 일부에 매달려 있다고 생각합니다. shutdown을 호출하면 모든 트래픽이 중지 된 다음 close를 사용하여 소켓을 종료합니다.

#!/usr/bin/python 

import logging 
import os 
import socket 
import sys 

def network_notify(name): 
    hosts = ['192.168.0.4', '192.168.0.6'] #server IP 
    port = 50000 
    size = 1024 
    title = 'Torrent Downloaded' 

    for host in hosts: 
     try: 
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
      # Set the socket to timeout after 10 seconds 
      s.settimeout(10.0) 
      s.connect((host, port)) 
     except socket.error, socket.timeout: 
      logging.error('Something is wrong with host: %s' % host) 
      continue 

     # Send the data 
     s.sendall('"%s" "%s"' % (title, name)) 
     data = s.recv(size) 

     # Shutdown and close the socket 
     s.shutdown(socket.SHUT_RDWR) 
     s.close() 

    if len(sys.argv) > 1: 
     name = ' '.join(sys.argv[1:]) 
     network_notify(name) 
    sys.exit() 
+0

트릭을 해 주셔서 감사합니다. 그래서 제대로 연결을 만들지 않았거나 올바르게 닫지 않았습니다. 대단히 감사드립니다. – James

관련 문제