我已经创建了一个小的python脚本,当在ubuntu12.04机器上使用uTorrent下载torrent时发送通知。当我运行pgrep -l torrent时,我可以看到大量的scipt,但无法杀死它们。utorrent调用传递torrent名称的脚本。该脚本工作正常,从终端运行时退出,但在utorrent调用它时不会关闭。我尝试将sys.exit()添加到脚本的底部,但它没有停止进程,也停止了通知的发送。
我能做些什么让他靠近吗?另外,我有什么办法可以终止这些进程吗?我试着杀了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发布于 2012-08-04 15:25:20
试一试,在运行脚本时,确保在调用脚本时执行/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()https://stackoverflow.com/questions/11805219
复制相似问题