Stack Overflow Asked by Saleel Almajd on November 20, 2021
i am trying to get message from a tcp/ip
i used this code coped from http://pymotw.com/2/socket/tcp.html
server
#!/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()
client
#!/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()
put i want to enter the message from text box
and receive it in a text box too
i tried to use python GTK
put i cant do a loop
also i tried to use Tkinter
put also i cant use it in a loop
can any one help me ??
Minimal client gui using Gtk. Sends user input to the server and puts the response into a label.
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()
Answered by Aran-Fey on November 20, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP