Pergunta

I've created a small python script to send a notification when a torrent is downloaded with uTorrent on an ubuntu 12.04 box. When I run pgrep -l torrent I can see loads of the scipt and am unable to kill them. utorrent calls the script passing the torrent name. The script works fine and exits when run from a terminal but doesnt close when utorrent calls it. I tried adding sys.exit() to the bottom of the script but it didnt stop the process and stopped the notification from being sent.

Aything I can do to make the scipt close? and also any Ideas how I can kill these processes? I tried kill pkill and htop.

Thanks.

#!/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 the pgrep

james@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
Foi útil?

Solução

Try this out and when you run the script make sure when it's called you do /path/to/script.py >> debug.txt for utorrent debug saving. This should give you better error handling and explicitly close the socket when done. I've also added a small timeout as well.

I'm guessing the socket is the hanging in a portion of the script. calling shutdown stops all traffic, then close will close off the socket.

#!/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()
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top