##language:zh #pragma section-numbers on ''' 中文Python行者首页 文章模板 ''' ## 仅仅是示范,你完全可以利用 MoinMoin 写你想写的! -- Chaox (<>) <> = 关于我 = ''个人信息'' |||| Chaox|| ||邮件|| chaox.maillist[at]gmail.com || ||工作电话|| || ||移动电话|| || ||UC 号码|| || ||MSN|| chaox08[at]hotmail.com || == 工作日志 == ||<^><>||<^>'''日志内容提要'''<>|| == 能力自述 == ''刚刚开始学Python,希望聆听和参与大家的讨论'' === 技术 === ==== 实例1 ==== '''新写了一个基于C/S的聊天工具,用了Threading,Pmw,socket 等''' Server : {{{#!python # ------------------------------------------------- # set up a server that will receive a connection # from a client, send a string to the client # and close the connection # Created by Chao Xiong 2005.Oct. # Version 1.1 # ------------------------------------------------- import socket import threading HOST = "" PORT = 4000 # HOST = raw_input("Pls input the IP address of host: ") # PORT = int(raw_input("Pls input the port of host: ")) # clientcounter = 1 clientnumber = 2 clients = [] buf = 1024 class client(threading.Thread): def __init__(self, server, ID): threading.Thread.__init__(self) self.connection = connections[ID] # self.friendcon = connection[1-ID] self.server = server self.ID = ID # self.friendID = 1 - ID def run(self): friendMessage = "" self.connection.send("Welcome to the Server! Client %s" % self.ID) friendMessage = self.connection.recv(buf) while 1: if friendMessage != "": for con in connections: con.send("Client %s : " % self.ID + friendMessage) # self.connection.send("Client %s : " % self.ID + friendMessage) # self.friendcon.send("Client %s : " % self.ID + friendMessage) print "Client %s : " % self.ID + friendMessage friendMessage = "" try: friendMessage = self.connection.recv(buf) except: print "Client %s is quit" % self.ID break class ChatServer: def __init__(self): self.HOST = "" self.PORT = 4000 self.address = (self.HOST, self.PORT) self.clientcounter = 0 self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server.bind(self.address) self.display("Waiting for connection...") self.execute() def execute(self): self.clients = [] global connections connections = [] destination = [] Id = 0 while 1: self.server.listen(clientnumber) con, dest = self.server.accept() connections.append(con) destination.append(dest) self.clientcounter += 1 self.clients.append(client(self.server, Id)) self.clients[Id].start() # self.clients[i].run() self.display("Client%s is connected" % Id) Id += 1 def display(self, message): print message def main(): ChatServer().execute() if __name__ == "__main__": main() }}} Client : {{{#!python # --------------------------------- # chatting tools Client GUI # Created by Chao Xiong 2005.Oct # version 1.0 # --------------------------------- from Tkinter import * import Pmw import threading import socket buf = 1024 class ChatClient(Frame, threading.Thread): """A simple GUI for client""" def __init__(self, name = "newcomer"): """initialize the frame""" self.name = name self.thecontents = "" threading.Thread.__init__(self) Frame.__init__(self) self.pack(expand = YES, fill = BOTH) # self.master.title("Chatting %s" % self.name) self.master.title("Chatting %s" % self.name) self.master.geometry("400x300") self.chatting = Entry(self) self.chatting.pack(fill = X, padx = 5, pady = 5) self.chatting.bind("", self.sendmessage) self.contents = Pmw.ScrolledText(self, text_state = DISABLED) self.contents.pack(expand = NO, fill = BOTH, padx = 5, pady = 5) self.start() def sendmessage(self, event): self.thecontents = event.widget.get() if self.thecontents != "": self.chatting.delete(0,len(self.thecontents)) self.connection.send(self.thecontents) def displaymessage(self, message): self.contents.text_state = NORMAL self.contents.appendtext(message) self.contents.appendtext("\n") self.contents.text_state = DISABLED def run(self): HOST = "127.0.0.1" PORT = 4000 address = (HOST, PORT) self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: self.connection.connect(address) except: self.displaymessage("Call failed") serverMessage = self.connection.recv(buf) while 1: if serverMessage != "": self.displaymessage(serverMessage) serverMessage = "" try: serverMessage = self.connection.recv(buf) except: self.displaymessage("The Server is shut down!") break self.connection.close() def main(): ChatClient().mainloop() if __name__ == "__main__": main() }}} 长是长了点,有些是废话。。。。 '''有个问题请教各位高手,现在在Windows下执行以上两段程序时(直接双击),除了出现 Pmw的窗口界面外,还会出现一个DOS窗口,不知怎么去掉这个DOS窗口??谢谢!''' * 把文件的后缀改为.pyw,然后双击就不会出现dos窗口了。 -- [[limodou]] ==== 实例2(24点算法) ==== 试着写了一个24点的程序,遍历所有的解法,最多是24×64×5=7680种可能。有点丑陋而且重解很多,不过还是发上来让大家扔一下。。。python真的是很强大啊~~ {{{ #!python from __future__ import division from operator import add, sub, mul from time import * div = lambda x, y : x/y ops = [add, sub, mul, div] opsc = ['+','-','*','/'] equation = [] data = input('Pls input the four numbers:(separate them by comma)') xtime = time() #-------------------------------------------------------- # list all the sort according to the four numbers, it is # copied from AllStartFromGame at http://python.cn #-------------------------------------------------------- def permute(seq): l = len(seq) if l == 1: return [seq] else: res=[] for i in range(len(seq)): rest = seq[:i] + seq[i+1:] for x in permute(rest): res.append(seq[i:i+1] + x) return res listofall = permute(data) newlist = list(set(listofall)) #-------------------------------------------------------- # consider the five different structures of combinations #-------------------------------------------------------- for a in newlist: for i in range(4): for j in range(4): for k in range(4): express = [] value = (a[0],opsc[i],a[1],opsc[j],a[2],opsc[k],a[3]) express.append("((%d %s %d) %s %d) %s %d" % value) express.append("(%d %s (%d %s %d)) %s %d" % value) express.append("%d %s ((%d %s %d) %s %d)" % value) express.append("%d %s (%d %s (%d %s %d))" % value) express.append("(%d %s %d) %s (%d %s %d)" % value) for s in express: try: x = eval(s) except: continue if x < 24.00001 and x > 23.99999: equation.append(s) ytime = time() cacltime = ytime - xtime equation = list(set(equation)) for e in equation: print e print "there are %d" % len(equation), "items at all" print "all the time of calculation is %s s" % cacltime }}} ==== 实例3(24点游戏) ==== 把24点的程序变成了游戏,适合学龄儿童和老年人玩,开发智力,保持思考。 :) 不过还是有bug,如不能接受小键盘输入。。。 如果大家发现bug,请写在下面,或者和我联系,谢谢~~ 如果不喜欢那些图片,直接删掉就可以~图片是从网上当的,如果侵犯了谁的权利,请告知,我立刻撤了~ [[attachment:24pGameV5_source.zip]] [[attachment:24pGameV5_exe.zip]] === 知识 === === 等等 === = 反馈 = ''欢迎大家浇水啊!''