inkedmn
11-13-2002, 07:40 AM
he posted a question on lno, thought my response might be something the python n00bs could learn from.
this is a really simple server that returns the current date/time whenever anything connects to it, and a client to test it:
the SERVER:
# simple socket communication example
# server listens for connections and returns the current date/time
# SERVER
import socket
import time
host = "localhost" # set host name
port = 1234 # set port number
# create socket object
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind host, port to socket object
sock.bind((host,port))
# set socket object to listen for up to 5 connections
sock.listen(5)
# cute status message
print "waiting for connection"
# socket.accept returns a tuple of the connection, an address
conn, addr = sock.accept()
# another cute status message
print addr, "connected"
# while connection is active
while 1:
# receives up to 1024 bytes of data
data = conn.recv(1024)
# if there's no data, break from the while loop
if not data: break
# send the current date/time back to the client
conn.send(time.ctime())
# close the socket
conn.close()
the CLIENT:
# simple socket communication example
# client connects to server we created
# CLIENT
import socket
host = "localhost" # set host name (same as server)
port = 1234 # set port number
# create socket object
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect to host on port (to the server)
sock.connect((host, port))
# send some data
sock.send("what time is it?")
# receive data up to 1024 bytes
data = sock.recv(1024)
# print a nicely formatted response
print "Server response:", data
# close the socket
sock.close()
anyway, just thought somebody might learn from this. i'll move it to tutorials if that's the general consensus (don't know if it'd call this a "tutorial" though... :))
this is a really simple server that returns the current date/time whenever anything connects to it, and a client to test it:
the SERVER:
# simple socket communication example
# server listens for connections and returns the current date/time
# SERVER
import socket
import time
host = "localhost" # set host name
port = 1234 # set port number
# create socket object
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind host, port to socket object
sock.bind((host,port))
# set socket object to listen for up to 5 connections
sock.listen(5)
# cute status message
print "waiting for connection"
# socket.accept returns a tuple of the connection, an address
conn, addr = sock.accept()
# another cute status message
print addr, "connected"
# while connection is active
while 1:
# receives up to 1024 bytes of data
data = conn.recv(1024)
# if there's no data, break from the while loop
if not data: break
# send the current date/time back to the client
conn.send(time.ctime())
# close the socket
conn.close()
the CLIENT:
# simple socket communication example
# client connects to server we created
# CLIENT
import socket
host = "localhost" # set host name (same as server)
port = 1234 # set port number
# create socket object
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect to host on port (to the server)
sock.connect((host, port))
# send some data
sock.send("what time is it?")
# receive data up to 1024 bytes
data = sock.recv(1024)
# print a nicely formatted response
print "Server response:", data
# close the socket
sock.close()
anyway, just thought somebody might learn from this. i'll move it to tutorials if that's the general consensus (don't know if it'd call this a "tutorial" though... :))