python搭建服务器和客户端

服务器代码:

import socket
if __name__ == '__main__':   
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('', 8001))
    sock.listen(5) 
    while True: 
        connection,address = sock.accept()
        try: 
            connection.settimeout(5) 
            buf = connection.recv(1024) 
            if buf == 'A': 
                connection.send('welcome to Building A!')
            if buf == 'B': 
                connection.send('welcome to Building B!')
            if buf == 'C': 
                connection.send('welcome to Building C!')
            if buf == 'D': 
                connection.send('welcome to Building D!')
            else: 
                connection.send('please go out!') 
        except socket.timeout: 
            print 'time out' 
        connection.close() 
客户端代码(有界面):

import wx
import socket
import time
class MainFrame(wx.Frame):
       
    def __init__(self,parent):
        self.title="Client Test Tool"
        wx.Frame.__init__(self, parent, -1, self.title, size=(800, 800))       
        panel = wx.Panel(self, -1)

        menubar=wx.MenuBar()
        clearLog=wx.Menu()
        save=wx.Menu()
        help=wx.Menu()
        menubar.Append(clearLog,"&Clear Log")
        clearLog.Append(1,"&Clear Server Log",'Clear Server log')
        clearLog.AppendSeparator()
        clearLog.Append(2,"&Clear Client Log",'Clear Client log')
        clearLog.AppendSeparator()
        clearLog.Append(3,"&Clear all Log",'Clear all log')
        menubar.Append(save,"&Save")
        save.Append(4,"&Save Server Log",'Save Server Log')
        save.AppendSeparator()
        save.Append(5,"&Save Client log",'Save Client log')
        menubar.Append(help,"&Help")
        help.Append(6,"&About")
        help.AppendSeparator()
        help.Append(7,"&Exit",'exit')
       
        self.SetMenuBar(menubar)

        self.Bind(wx.EVT_MENU,self.ClearServerLog,id=1)
        self.Bind(wx.EVT_MENU,self.ClearClientLog,id=2)
        self.Bind(wx.EVT_MENU,self.ClearAllLog,id=3)
        self.Bind(wx.EVT_MENU,self.SaveServerLog,id=4)
        self.Bind(wx.EVT_MENU,self.SaveClientLog,id=5)
        self.Bind(wx.EVT_MENU,self.AboutT,id=6)
        self.Bind(wx.EVT_MENU,self.OnClose,id=7)
       
        #Client input
        self.RequestList=['A','B','C','D','F']
        Client_Input_lable=wx.StaticText(panel,-1,"Client input",pos=(25,10))
        #self.Client_Input_Text=wx.Choice(panel, -1, "",pos=(25,30))
        self.Client_Input_Text=wx.ComboBox(panel, -1,'A', choices=self.RequestList,pos=(25,30),size=(100,20))
        Client_Button=wx.Button(panel, -1, "Connect Server",pos=(25,70))
        self.Bind(wx.EVT_BUTTON,self.Connect_Server, Client_Button)

        #Auto Test
        Auto_Button=wx.Button(panel, -1, "Auto Test",pos=(300,80))
        self.Bind(wx.EVT_BUTTON,self.Auto_Test, Auto_Button)
        Auto_Test_Label=wx.StaticText(panel,-1,"Auto times: ",pos=(250,50))
        self.Auto_Test_Text = wx.TextCtrl(panel, -1, size=(100,20),pos=(320,50))
       
        #Server Log
        Server_msg_Label=wx.StaticText(panel,-1,"Server Log information",pos=(20,120))
        self.Server_msg_Text = wx.TextCtrl(panel, -1, size=(350,500),pos=(20,140),style= wx.TE_MULTILINE |wx.HSCROLL|wx.TE_WORDWRAP|wx.TE_READONLY)

        #Client Log
        Client_msg_Label=wx.StaticText(panel,-1,"Client Log information",pos=(410,120))
        self.Client_msg_Text = wx.TextCtrl(panel, -1, size=(350,500),pos=(410,140),style= wx.TE_MULTILINE |wx.HSCROLL|wx.TE_WORDWRAP|wx.TE_READONLY)

        self.Centre()
        self.Show(True)

       
    def ClearServerLog(self,event):
        self.Server_msg_Text.Clear()
       
    def ClearClientLog(self,event):
        self.Client_msg_Text.Clear()
       
    def ClearAllLog(self,event):
        self.Server_msg_Text.Clear()
        self.Client_msg_Text.Clear()
       
    def SaveServerLog(self,event):
        print "SaveServerLog"
    def SaveClientLog(self,event):
        print "SaveClientLog"
    def OnClose(self,event):
        self.Close()
       
    def AboutT(self,event):
        string= "This is a tool can get the information between client and server"
        dlg=wx.MessageBox(string)
       
   
    def Connect_Server(self,event):
        Client_Input_Value=self.Client_Input_Text.GetValue()
        Server_Log=time.ctime()+": "+Client_Input_Value+"\n"
        self.Server_msg_Text.AppendText(Server_Log)
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
        sock.connect(('localhost', 8001))
        #sock.connect(('192.168.1.5', 8001))  #Server IP
        time.sleep(2) 
        sock.send(Client_Input_Value)
        Rec=time.ctime()+": "+sock.recv(1024)+"\n"
        self.Client_msg_Text.AppendText(Rec)
        time.sleep(2)
        sock.close()

    def Auto_Test(self,event):
        Auto_Time=self.Auto_Test_Text.GetValue()       
        for i in range(int(Auto_Time)):
            self.Connect_Server(self)
   
if __name__=="__main__":
    app=wx.App()
    frame = MainFrame(None)
    frame.Show(True)
    app.MainLoop()

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值