首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

写了个客户端程序总是卡,要不就发送数据失败,求

2012-09-17 
写了个客户端程序总是卡,要不就发送数据失败,求高手指点服务器程序:import socketimport timeimport MySQL

写了个客户端程序总是卡,要不就发送数据失败,求高手指点
服务器程序:
import socket
import time
import MySQLdb
host='212.155.144.165'
port=34586
bufsiz=1024
ADDR=(host,port)
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.bind(ADDR)
s.listen(5)
while 1:
  print 'waiting for connection...'
  data=sc.recv(bufsiz)
  if data=='load':
  sc.send('ok')
  x=‘0’
  sc,addr=s.accept()
  data1=sc.recv(bufsiz)
  a=data1.split('/')
  db=MySQLdb.connect(host='localhost',user='root',
  passwd='123',db='python')
  cur=db.cursor()
  r=cur.execute('select usernumber from user')
  r=cur.fetchall()
  ns=[]
  for rec in r:
  ns.append(rec)
  n=[]
  for i in range(0,len(ns)):
  n.append(ns[i][0])
  if a[0] in n:
  p=cur.execute("select password from user where usernumber=%s"%n)
  p=cur.ferchone()
  ns1=[]
  for rec in p:
  ns1.append(rec)
  if a[1]==ns1[0]:
  x = ‘1’
  break
  s.send(x)
  cur.close()
  db.close()
sc.close()
s.close()




客户端程序:
import socket
import wx
import wx.grid
import MySQLdb
host='212.155.144.165'
port=34586
bufsiz=1024
ADDR=(host,port)
sc=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sc.connect(ADDR)
#登录界面
class LoginDialog(wx.Frame):
 
  def __init__(self):
  self.logindialog=wx.Frame.__init__(self, None, -1, "A202 系统", size=(300, 300))
  panel = wx.Panel(self, -1)
  label1=wx.StaticText(panel,-1,'用户登录',pos=(120,20))
  label2=wx.StaticText(panel,-1,'用户名:',pos=(10,50))
  self.name=wx.TextCtrl(panel,-1,'211148',pos=(80,50),size=(160,-1))
  label3=wx.StaticText(panel,-1,"密码:",pos=(10,100))
  self.password=wx.TextCtrl(panel,-1,"1234",pos=(80,100),size=(160,-1),
  style=wx.TE_PASSWORD)
  sampleList = ['管理员', '普通用户']
  wx.StaticText(panel, -1, "用户类型:", (10,150))
  wx.Choice(panel, -1, (80, 150), choices=sampleList)
  self.button1=wx.Button(panel,-1,'确定',pos=(50,200))
  self.button2=wx.Button(panel,-1,'取消',pos=(150,200))
  self.Bind(wx.EVT_BUTTON, self.OnButton1, self.button1)
  self.Bind(wx.EVT_BUTTON, self.OnButton2, self.button2)
  self.button1.SetDefault()
  def OnButton1(self,event):
  sc.send('load')
  data=sc.recv(bufsiz)
  if data=='ok':
  logindlg=LoginDialog()
  data1=self.name.GetValue()
  data2=self.password.GetValue()
  data3=data1+'/'+data2
  sc.send(data3)
  y=sc.recv(bufsiz)
  if y=='1':
  frame=mainframe(self)
  frame.Show()
  logindlg.Destroy()
  else:
  wx.MessageBox('用户名不存在或密码错误,请重新输入!', '提示信息', wx.OK|wx.ICON_INFORMATION)
  logindlg.Destroy()


  def OnButton2(self,event):
  self.Close()
class mainframe(wx.Frame):
。。。。。。。。。
if __name__ == '__main__':
  app = wx.PySimpleApp()
  frame=LoginDialog()
  frame.Show(True)
  app.MainLoop()

[解决办法]
看到你跟我最近要做的工具有些类似,我就贴了你的代码修改了下,
我的电脑没装数据库所以把相关的都注掉了,你试下
服务器:

Python code
import socketimport time#import MySQLdbhost='192.168.24.212'port=34586bufsiz=1024ADDR=(host,port)s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)s.bind(ADDR)s.listen(5)while 1:  print 'waiting for connection...'  sc,addr=s.accept()  data=sc.recv(bufsiz)  print 'data:',data  if data=='load':    sc.send('ok')    x='0'    data1=sc.recv(bufsiz)    print 'data1:',data1    a=data1.split('/')    #function = functemp.split(':')    #db=MySQLdb.connect(host='localhost',user='root',    #passwd='123',db='python')    #cur=db.cursor()    #r=cur.execute('select usernumber from user')    #r=cur.fetchall()    #ns=[]    #for rec in r:    #  ns.append(rec)    #  n=[]    #for i in range(0,len(ns)):    #  n.append(ns[i][0])    #if a[0] in n:    #  p=cur.execute("select password from user where usernumber=%s"%n)    #  p=cur.ferchone()    #  ns1=[]    #for rec in p:    #  ns1.append(rec)    #if a[1]==ns1[0]:    #  x = '1'    #  break    sc.send('1')    print '1111111'    #cur.close()    #db.close()sc.close()s.close()
[解决办法]
服务器那块有问题,客户端发送数据:sc.send(data3)发送不过去,阻塞了,

感觉我很啰嗦,你试试吧,我也是菜鸟~~~

热点排行