2014-12-21 13 views
-1

我试图从TCP/IP我怎样才能从TCP/IP的消息,汽车无文本框

得到消息,我用这个代码http://pymotw.com/2/socket/tcp.html

服务器

应对打印
#!/usr/bin/env python 
#server 

import socket 
import sys 

# Create a TCP/IP socket 
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
# Bind the socket to the port 
server_address = ('localhost', 10000) 
print >>sys.stderr, 'starting up on %s port %s' % server_address 
sock.bind(server_address) 
# Listen for incoming connections 
sock.listen(1) 

while True: 
    # Wait for a connection 
    print >>sys.stderr, 'waiting for a connection' 
    connection, client_address = sock.accept() 
try: 
    print >>sys.stderr, 'connection from', client_address 

    # Receive the data in small chunks and retransmit it 
    while True: 
     data = connection.recv(16) 
     print >>sys.stderr, 'received "%s"' % data 
     if data: 
      print >>sys.stderr, 'sending data back to the client' 
      connection.sendall(data) 
     else: 
      print >>sys.stderr, 'no more data from', client_address 
      break 

finally: 
    # Clean up the connection 
    connection.close() 

客户

#!/usr/bin/env python 
#client 

import socket 
import sys 

# Create a TCP/IP socket 
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

# Connect the socket to the port where the server is listening 
server_address = ('192.168.1.88', 10000) 
print >>sys.stderr, 'connecting to %s port %s' % server_address 
try: 
    sock.connect(server_address) 
    print >>sys.stderr, 'connecting good' 
finally: 
    print >>sys.stderr, 'connecting failed' #% server_address 

try: 

    # Send data 
    message = 'This is the message. It will be repeated.' 
    print >>sys.stderr, 'sending "%s"' % message 
    sock.sendall(message) 

    # Look for the response 
    amount_received = 0 
    amount_expected = len(message) 

    while amount_received < amount_expected: 
     data = sock.recv(16) 
     amount_received += len(data) 
     print >>sys.stderr, 'received "%s"' % data 

finally: 
    print >>sys.stderr, 'closing socket' 
    sock.close() 

放我想从输入文本框0123的消息并在文本框中收到过

我试图使用Python GTK 放我不能做一个循环

还我试图用Tkinter 放还我不能在循环使用它

谁能帮我 ??

+0

为什么不把所有的代码放在一个函数中,在你选择的GUI工具包中创建一个按钮,并在用户单击按钮时执行该函数?你的问题到底是什么? –

+0

我想让它听听从tcp收到的msg并自动在gui上打印它@Rawing –

回答

0

使用Gtk的最小客户端gui。将用户输入发送到服务器并将响应放入标签中。

from gi.repository import Gtk, Gdk, GLib 
import socket 

# create the connection 
sock= socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
sock.connect(('localhost', 10000)) 


def send(button): 
    text= entry.get_text() # get the text to send from the GtkEntry 
    # send the stuff 
    remaining= len(text) 
    while True: 
     sent= sock.send(text) 
     remaining-= sent 
     if not remaining: 
      break 
     text= text[sent:] 
    # wait for the server's response 
    response= sock.recv(50) 
    # and put it into a label 
    Gdk.threads_add_idle(GLib.PRIORITY_DEFAULT, label.set_text, response) 

# spawn the GUI 
window= Gtk.Window() 
vbox= Gtk.VBox() 
entry= Gtk.Entry() 
button= Gtk.Button('send') 
# when the user clicks the button, send something to the server 
button.connect('clicked', send) 
label= Gtk.Label() 
vbox.pack_start(entry, False, False, 0) 
vbox.pack_start(button, False, False, 0) 
vbox.pack_start(label, False, False, 0) 
window.add(vbox) 

window.connect('delete-event', Gtk.main_quit) 
window.show_all() 
Gtk.main()