求高手指教,python中C/S传输字符串问题
问题如下:代码编译过可以通过,server和client传输时,直接传一个字符串可以,但我将它换成文本框中
的内容connection.send(text)就传不过去了,我想问下是不是send中text参数不应该这么传进去,以前一直
用c/c++,刚接触python,求多指教。
我的server.py
from Tkinter import *import socketdef on_clicked(): while True: connection, address = sock.accept() try: connection.settimeout(4) buf = connection.recv(1024) label['text'] = buf connection.send(text) except socket.timeout: print 'time out' connection.close()root = Tk(className = 'Server')label = Label(root)label['text'] = 'IP'label.pack()text = StringVar()text.set('input ip')entry = Entry(root)entry['textvariable'] = textentry.pack()sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)#sock.bind((text, 8000))sock.bind(('localhost', 8001))sock.listen(5)button = Button(root)button['text'] = 'OK'button['command'] = on_clickedbutton.pack()root.mainloop()
from Tkinter import *import socketimport timedef on_clicked(): sock.connect(('localhost', 8001)) time.sleep(2) sock.send(text) recv = sock.recv(1024) label['text'] = recv sock.close()root = Tk(className = 'Client')label = Label(root)label['text'] = 'connect to...'label.pack()sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)text = StringVar()text.set('IP')entry = Entry(root)entry['textvariable'] = textentry.pack()button = Button()button['text'] = 'connect'button['command'] = on_clickedbutton.pack()root.mainloop()
def on_clicked(): while True: button['text']='running...' root.update() connection, address = sock.accept() try: connection.settimeout(4) buf = connection.recv(1024) label['text'] = buf.decode('gbk') root.update() sendbuf=entry.get() connection.send(sendbuf) except socket.timeout: print 'time out' connection.close()