Saturday, September 19, 2009

Python - a simple client using socket

# simple client sends and receives
# ensure that the port you are sending to is

#  the same port your server is listening on!
#
# use this script in conjunction with the simple echo server


import socket


sendMssg = "Hello World!"
host = 'localhost'

port = 54321
size = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))
s.send(sendMssg)
print 'Sent:', sendMssg

data = s.recv(size)
s.close()

print 'Received:', data

# output:
#  Sent: Hello World!
#  Received: Hello World!

No comments:

Post a Comment