go.py
上传用户:wxmbxm
上传日期:2022-08-10
资源大小:3k
文件大小:3k
- #!/usr/bin/env python
- import pygtk
- pygtk.require('2.0')
- import gtk, gobject, gtk.glade, pango
- import socket, sys, select, struct
- HOST="0.0.0.0"
- SENDADD=""
- SENDPORT=5001
- timeout=100
- packet=''
- def get_local_addr():
- try:
- import fcntl
- s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- return socket.inet_ntoa(fcntl.ioctl(sock_listen.fileno(), 0x8915, struct.pack('256s', 'eth0'))[20:24])
- except:
- return '192.168.1.1' #呵呵
- def get_local_broadcast_addr():
- tmp1 = get_local_addr()
- tmp2 = tmp1.split('.')
- tmp2[3] = '255'
- return '.'.join(tmp2)
- def on_keypress(widget, event): #处理回车键和Esc键
- if event.keyval == gtk.keysyms.Escape:
- win.iconify()
- if event.keyval == gtk.keysyms.Return:
- on_button1_clicked(None)
- def on_button1_clicked(button): #点击发送按钮
- global send_socket
- tv = textv
- input_str = entry_send.get_text()
- if len(input_str) <= 0:
- return
- #get text string need send
- string = entry_name.get_text() + ': ' + input_str + 'n'
- #clean input entry
- entry_send.set_text('')
- #send out now!
- send_socket.sendto(string, (SENDADD, SENDPORT))
- def write_tv(data): #把受到的数据显示到文本框
- #write to textview
- gtk.gdk.threads_enter()
- tv = textv
- tvbuffer = tv.get_buffer()
- iter_last = tvbuffer.get_iter_at_line(tvbuffer.get_line_count())
- tvbuffer.insert(iter_last, data)
- tv.scroll_to_iter(iter_last, tv.get_left_margin(), False, 0.5, 0.5)
- gtk.gdk.threads_leave()
-
- def get_data_from_sock(): #从socket收数据,没有数据就立刻返回
- global packet
- a,b,c=select.select([sock_listen.fileno()],[],[],0)
- if len(a) <= 0:
- return 1
- packet, address = sock_listen.recvfrom(1024)
- write_tv(packet)
- packet = ''
- return 1
- #
- #程序从这里开始
- #
- #create listen socket
- try:
- sock_listen = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- sock_listen.bind((HOST, SENDPORT))
- except socket.error:
- sys.exit("bind %d port error" % SENDPORT)
- #得到广播地址,如果是windows系统就不能工作,因为windows没有fcntl模块
- SENDADD = get_local_broadcast_addr()
- #每100毫秒检查一次是否有人发消息给我
- gobject.timeout_add(timeout, get_data_from_sock)
- #create send socket
- try:
- send_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- send_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) #设置为广播模式的socket
- except socket.error:
- sys.exit("bind %d port error" % PORT)
- #从XML文件里把图形控件都拿出来,并设置信号处理函数
- xml = gtk.glade.XML('office-talk.glade')
- win = xml.get_widget('window1')
- button = xml.get_widget("button1")
- textv = xml.get_widget('tv1')
- entry_send = xml.get_widget('entry1')
- entry_name = xml.get_widget('entry2')
- #add signal handle
- win.connect('destroy', gtk.main_quit)
- button.connect("clicked", on_button1_clicked)
- #catch Escape&Return, sign... it's really a stupid way
- entry_send.connect('key_press_event', on_keypress)
- textv.connect('key_press_event', on_keypress)
- win.connect('key_press_event', on_keypress)
- entry_name.connect('key_press_event', on_keypress)
- button.connect('key_press_event', on_keypress)
- try:
- gtk.main()
- finally:
- sys.exit()