免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4160 | 回复: 4
打印 上一主题 下一主题

limodou大哥请进--关于BitTorrent中获取PID并写入文件的那几行代码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-12-20 10:10 |只看该作者 |倒序浏览
是这样的,因为当时架设BT服务器,使用的是BitTorrent,为了满足自己的需要,就修改了两行代码,使得bttracker.py程序可以自动将PID写入'/var/run/bttrack.pid'中。
代码是正确的,所以我把它提交给了BitTorrent的官方邮件地址 'bugs@bittorrent.com',第二天我就收到了作者的回复。这是我发的邮件原文和他的回复,您看了就明白了:
Require a new feature -- Get bttrack.py's PID and write it to a file(/var/run/bttrack.pid).

A week ago,I installed BitTorrent on FreeBSD 5.4 as a BT server,but i wanna a shell script to start and
stop the tracker expediently,just like those scripts under '/usr/local/etc/rc.d/".

I wanna i can get the bttrack.py's PID from a file after it start,it make stop the server expediently.

So ,i modified the 'bttracker.py',added two lines to get the tracker's PID and wrote it to '/var/run/bttrack.py'.
It's dirty code :
=========== bttrack.py ================

  1. from sys import argv
  2. from BitTorrent.track import track

  3. import os     # added

  4. if __name__ == '__main__':
  5.     file('/var/run/bttrack.pid', 'w').write(str(os.getpid()))    # added
  6.     track(argv[1:])
复制代码

=====================================

and i wrote a shell script 'bttrack.sh ',put it under '/usr/local/etc/rc.d' :

============ bttrack.sh ===================

  1. #!/usr/local/bin/bash

  2. # Author : MichaelBibby ( michaelbibby # gmail.com )
  3. # Date : 2005/12/15
  4. # Purpose : To start '/usr/local/bin/bttrack.py' at system startup.

  5. BTTRACK='/usr/local/bin/bttrack.py'
  6. BTTRACK_PID_FILE='/var/run/bttrack.pid'
  7. BTTRACK_PID=$(cat $BTTRACK_PID_FILE)

  8. BTTRACK_ARGS='--port 6969 --dfile /var/log/bttrack/dlinfo --allowed_dir
  9. /home/torrents --show_infopage 0 --logfile /var/log/bttrack/bttrack.log'

  10. function USAGE()
  11. {
  12.     echo -e "\n\t Usage : $0 [start|stop]"
  13. }

  14. # ---- Main ------
  15. if [ X$# == X0 ]
  16. then
  17.     $BTTRACK $BTTRACK_ARGS &
  18. elif [ X$# == X1 ]
  19. then
  20.     case $1 in
  21.         start) echo -ne "Starting BT tracker ..."
  22.             $BTTRACK $BTTRACK_ARGS &
  23.             echo -ne "\t Done."
  24.         ;;
  25.         stop) echo -ne "Stop BT tracker ..."
  26.             kill $BTTRACK_PID
  27.             echo -ne "\t Done."
  28.         ;;
  29.         *) USAGE
  30.         ;;
  31.         esac
  32. else
  33.     USAGE
  34. fi
  35. # ----- Done ----
复制代码

=======================================

这是作者Matt Chisholm的回复(邮件原件):
Thanks so much, this has been on our to-do list for a while.  In order
for it to be included in BitTorrent, you'll have to agree to license
it under the BitTorrent Open Source License:

http://www.bittorrent.com/license/

and to assign copyright to BitTorrent.  Is this ok?
Of course, we'll add your name to the credits too.

matt

因为这两行代码都是您的作品,所以如果您同意它的license的话,我可以代表您向他回复邮件表示同意。


[ 本帖最后由 MichaelBibby 于 2005-12-20 17:57 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2005-12-20 10:13 |只看该作者
我发的邮件中,之所以说是“dirty code”,是因为‘import os'这一行我不知道该具体如何写

是from os import XXX呢,还是直接'import os'就行
您觉得哪个会更合适一些?
因为我看该脚本中都是'from xxx import xxx'的

论坛徽章:
0
3 [报告]
发表于 2005-12-20 10:38 |只看该作者
嘿嘿,小样英文进步不少哦。

论坛徽章:
0
4 [报告]
发表于 2005-12-20 12:43 |只看该作者
使用import os为好。这样容易让别人清楚名字空间。

另外作者就是你就可以,不必提到我。这是你的成果。

论坛徽章:
0
5 [报告]
发表于 2005-12-20 18:16 |只看该作者
原帖由 limodou 于 2005-12-20 12:43 发表
使用import os为好。这样容易让别人清楚名字空间。

另外作者就是你就可以,不必提到我。这是你的成果。

那我就越俎代庖了

  1. I agree to the license : http://www.bittorrent.com/license/ .

  2. But i think that the code is faultiness,we should add some more code to test host system type,e.g:FreeBSD/OpenBSD/NetBSD/Linux .

  3. The shell script works fine in FreeBSD,but it isn't perfect too.
  4. We should modify it in some place.
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP