【问题标题】:Python uTorrent notification script doesnt terminatePython uTorrent 通知脚本不会终止
【发布时间】:2012-08-04 01:33:46
【问题描述】:

我创建了一个小型 python 脚本,用于在 ubuntu 12.04 机器上使用 uTorrent 下载 torrent 时发送通知。当我运行 pgrep -l torrent 时,我可以看到大量的 scipt 并且无法杀死它们。 utorrent 调用传递种子名称的脚本。该脚本工作正常并在从终端运行时退出,但在 utorrent 调用它时不会关闭。我尝试将 sys.exit() 添加到脚本底部,但它没有停止进程并停止发送通知。

我能做些什么来让 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)

这是 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

【问题讨论】:

  • wchan 全部退出
  • 你的包罗万象的except: 子句可能隐藏了一些有用的信息。此外,socket 方法调用之一可能会挂起。添加一些日志以调试哪个日志可能很有用。

标签: python network-programming python-2.7 ubuntu-12.04 utorrent


【解决方案1】:

试试这个,当你运行脚本时,确保在调用它时你做/path/to/script.py >> debug.txt 用于保存 utorrent 调试。这应该为您提供更好的错误处理,并在完成后显式关闭套接字。我还添加了一个小超时。

我猜套接字是脚本的一部分。调用 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()

【讨论】:

  • 谢谢你成功了。所以它要么没有正确创建连接,要么没有正确关闭它。非常感谢,我不会想到这一点。
猜你喜欢
  • 2021-05-09
  • 2011-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-01
相关资源
最近更新 更多