初学python
kf701
|
1#
kf701 发表于 2008-01-14 11:52
初学python初学python,save it. kf701@kf701-laptop:~/python$ cat TCPServer.py #!/usr/bin/python # -*- coding= utf-8 -*- import SocketServer import sys class Handler(SocketServer.StreamRequestHandler): def handle(self): addr = self.request.getpeername() print 'connection from:', addr data = self.rfile.read(1024) print 'data:', data self.wfile.write("xxxx") # main function from here if len(sys.argv) kf701@kf701-laptop:~/python$ cat UDPServer.py #!/usr/bin/python # -*- coding= utf-8 -*- import SocketServer import sys class Handler(SocketServer.DatagramRequestHandler): def handle(self): print 'connection from:', self.client_address data = self.rfile.read(1024) print 'data:', data self.wfile.write("xxxx") # main function from here if len(sys.argv) kf701@kf701-laptop:~/python$ cat ThreadTCPServer.py #!/usr/bin/python # -*- coding= utf-8 -*- import SocketServer import sys class Handler(SocketServer.StreamRequestHandler): def handle(self): addr = self.request.getpeername() print 'connection from:', addr data = self.rfile.read(1024) print 'data:', data self.wfile.write("xxxx") # main function from here if len(sys.argv) kf701@kf701-laptop:~/python$ cat SelectTCPServer.py #!/usr/bin/python # -*- coding= utf-8 -*- # write by kf701 import socket import select class SelectTCPServer: def __init__(self, server_address, RequestHandlerClass): self.server_address = server_address self.RequestHandlerClass = RequestHandlerClass self.listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.listen_socket.bind( server_address ) self.listen_socket.listen(5) self.sock_list = [self.listen_socket] self.debug = False print "TCP server listen at", server_address def enable_debug(self): self.debug = True def disable_debug(self): self.debug = False def serve_forever(self): while True: rs, ws, es = select.select(self.sock_list, [], []) for sock in rs: if sock is self.listen_socket: client,addr = self.listen_socket.accept() if self.debug: print "connect", addr self.sock_list.append(client) continue try: data = sock.recv(1024) disconnected = not data except socket.error: disconnected = True if disconnected: self.sock_list.remove(sock) if self.debug: print "disconnect", sock.getpeername() else: self.RequestHandlerClass( sock, data ) if __name__ == '__main__': class Handle: def __init__(self, sock, data): self.sock = sock self.data = data self.handle() def handle(self): print self.data s = SelectTCPServer( ('', 9999), Handle ) s.enable_debug() s.serve_forever() kf701@kf701-laptop:~/python$ cat myToken.py #!/usr/bin/python # _*_ coding= utf_8 _*_ # By kf701 # Sat Jan 12 14:23:09 CST 2008 DELIMITER = [' ',',',';','(',')','{','}','[',']','='] SKIP_KEY = ['\r','\n','#'] class myToken: def __init__(self, filename): self.tokens = self.parse(filename) def get_tokens(self): return self.tokens def parse(self, filename): tokens = [] f = file(filename, "r") for line in f.readlines(): while line and line[0] not in SKIP_KEY: line = line.strip() i = 0 while i kf701@kf701-laptop:~/python$ cat TestSelect.py #!/usr/bin/python # -*- coding= utf-8 -*- from SelectTCPServer import SelectTCPServer if __name__ == '__main__': class Handle: def __init__(self, sock, data): self.sock = sock self.data = data self.handle() def handle(self): print self.data s = SelectTCPServer( ('', 9999), Handle ) s.debug = True s.serve_forever() kf701@kf701-laptop:~/python$ cat gtk_example2.py #!/usr/bin/env python #-*- encoding:utf-8 -*- import pygtk pygtk.require('2.0') import gtk class base: #destroy信号的回调函数 def destroy(self,widget,data=None): gtk.main_quit() #clicked信号的回调函数 def hello(self,widget,data): print 'hello ' + data + ' this is a button clicked() test' #delete_event事件的回调函数 def delete_event(self, widget, event, data=None): print "delete event occurred" #如果delete_event事件返回假,则会触发destroy信号,从而关闭窗口。 #如果返回真,则不会关闭窗口。这个特性在当我们需要一个确认是否退出的选择对话框时是很有用。 return False def __init__(self): self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) self.window.set_title('PyGTK 测试 window') self.window.set_default_size(200,200) self.window.set_border_width(100) self.window.set_position(gtk.WIN_POS_CENTER) #设置窗口的delete_event信号触发delete_event函数 self.window.connect("delete_event", self.delete_event) #设置窗口的destroy信号触发destroy函数 handler1 = self.window.connect("destroy",self.destroy) print "handler1 is:%d" % handler1 #生成按钮实例 self.button1 = gtk.Button() self.button2 = gtk.Button() self.button1.set_label('hello') self.button2.set_label('exit') #设置按钮的clicked信号触发hello函数,并传递‘pyGTK’字符串参数给hello函数 handler2 = self.button1.connect("clicked",self.hello,"pyGTK") print "handler2 is:%d" % handler2 #设置按钮的clicked信号触发self.window对象的gtk.Widget.destroy方法 self.button2.connect_object("clicked", gtk.Widget.destroy, self.window) #使用object.disconnect(id)方法取消handler2的功能 #self.button.disconnect(handler2) #设置一个不可见的横向的栏位self.box1 self.box1 = gtk.HBox(False, 0) #把box1放到窗口中 self.window.add(self.box1) #把button1部件放到box1中 self.box1.pack_start(self.button1,True,False,0) self.button1.show() #把button2部件放到button1部件之后 self.box1.pack_start(self.button2,False,False,0) self.button2.show() self.box1.show() self.window.show() def main(self): gtk.main() if __name__ == "__main__": base = base() base.main() |