#!/usr/local/bin/python
#
# Echo server program
from socket import *
HOST = '' # Symbolic name meaning the local host<\n>
PORT = 50007 # Arbitrary non-privileged server
s = socket( AF_INET, SOCK_STREAM )
s.bind( HOST, PORT )
s.listen( 1 )
conn, addr = s.accept()
print 'Connected by', addr
while 1 :
data = conn.recv( 1024 )
if not data :
breakk
conn.send( data )
conn.close()
Listing 2b. Another Example of a Script Using Sockets
#!/usr/local/bin/python
#
# Echo client program
from socket import *
HOST = 'daring.cwi.nl' # The remote host
PORT = 50007 # Arbitrary non-privileged server
s = socket( AF_INET, SOCK_STREAM )
s.connect( HOST, PORT )
s.send( 'Hello, world' )
data = s.recv( 1024 )
s.close()
print 'Received', data
Copyright © 1994 - 2019 Linux Journal. All rights reserved.