go.py
上传用户:wxmbxm
上传日期:2022-08-10
资源大小:3k
文件大小:3k
源码类别:

ICQ/即时通讯

开发平台:

Python

  1. #!/usr/bin/env python
  2. import pygtk
  3. pygtk.require('2.0')
  4. import gtk, gobject, gtk.glade, pango
  5. import socket, sys, select, struct
  6. HOST="0.0.0.0"
  7. SENDADD=""
  8. SENDPORT=5001
  9. timeout=100
  10. packet=''
  11. def get_local_addr():
  12.    try:
  13.       import fcntl
  14.       s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  15.       return socket.inet_ntoa(fcntl.ioctl(sock_listen.fileno(), 0x8915, struct.pack('256s', 'eth0'))[20:24])
  16.    except:
  17.       return '192.168.1.1' #呵呵
  18. def get_local_broadcast_addr():
  19.    tmp1 = get_local_addr()
  20.    tmp2 = tmp1.split('.')
  21.    tmp2[3] = '255'
  22.    return '.'.join(tmp2)
  23. def on_keypress(widget, event): #处理回车键和Esc键
  24.    if event.keyval == gtk.keysyms.Escape:
  25.        win.iconify()
  26.    if event.keyval == gtk.keysyms.Return:
  27.        on_button1_clicked(None)
  28. def on_button1_clicked(button): #点击发送按钮
  29.    global send_socket
  30.    tv = textv
  31.    input_str = entry_send.get_text()
  32.    if len(input_str) <= 0:
  33.       return
  34.    #get text string need send
  35.    string = entry_name.get_text() + ': ' + input_str + 'n'
  36.    #clean input entry
  37.    entry_send.set_text('')
  38.    #send out now!
  39.    send_socket.sendto(string, (SENDADD, SENDPORT))
  40. def write_tv(data): #把受到的数据显示到文本框
  41.    #write to textview
  42.    gtk.gdk.threads_enter()
  43.    tv = textv
  44.    tvbuffer = tv.get_buffer()
  45.    iter_last = tvbuffer.get_iter_at_line(tvbuffer.get_line_count())
  46.    tvbuffer.insert(iter_last, data)
  47.    tv.scroll_to_iter(iter_last, tv.get_left_margin(), False, 0.5, 0.5)
  48.    gtk.gdk.threads_leave()
  49.    
  50. def get_data_from_sock(): #从socket收数据,没有数据就立刻返回
  51.    global packet
  52.    a,b,c=select.select([sock_listen.fileno()],[],[],0)
  53.    if len(a) <= 0:
  54.       return 1
  55.    packet, address = sock_listen.recvfrom(1024)
  56.    write_tv(packet)
  57.    packet = ''
  58.    return 1
  59. #
  60. #程序从这里开始
  61. #
  62. #create listen socket
  63. try:
  64.    sock_listen = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  65.    sock_listen.bind((HOST, SENDPORT))
  66. except socket.error:
  67.    sys.exit("bind %d port error" % SENDPORT)
  68. #得到广播地址,如果是windows系统就不能工作,因为windows没有fcntl模块
  69. SENDADD = get_local_broadcast_addr()
  70. #每100毫秒检查一次是否有人发消息给我
  71. gobject.timeout_add(timeout, get_data_from_sock)
  72. #create send socket
  73. try:
  74.    send_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  75.    send_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) #设置为广播模式的socket
  76. except socket.error:
  77.    sys.exit("bind %d port error" % PORT)
  78. #从XML文件里把图形控件都拿出来,并设置信号处理函数
  79. xml = gtk.glade.XML('office-talk.glade')
  80. win = xml.get_widget('window1')
  81. button = xml.get_widget("button1")
  82. textv = xml.get_widget('tv1')
  83. entry_send = xml.get_widget('entry1')
  84. entry_name = xml.get_widget('entry2')
  85. #add signal handle
  86. win.connect('destroy', gtk.main_quit)
  87. button.connect("clicked", on_button1_clicked)
  88. #catch Escape&Return, sign... it's really a stupid way
  89. entry_send.connect('key_press_event', on_keypress)
  90. textv.connect('key_press_event', on_keypress)
  91. win.connect('key_press_event', on_keypress)
  92. entry_name.connect('key_press_event', on_keypress)
  93. button.connect('key_press_event', on_keypress)
  94. try:
  95.    gtk.main()
  96. finally:
  97.    sys.exit()