Server:
# Server
Import socket address
= (' 127.0.0.1 ', 31500)
s = socket.socket (socket.af_inet, socket. SOCK_STREAM) # s = Socket.socket ()
s.bind (address)
S.listen (5)
ss, addr = s.accept ()
print ' Got Connected from ', addr
ss.send (' Byebye ')
RA = ss.recv (r)
print RA
ss.close ()
s.close ()
Client:
# Client
Import socket Address
= (' 127.0.0.1 ', 31500)
s = socket.socket (socket.af_inet, socket. SOCK_STREAM)
s.connect
data = S.RECV ()
print ' The data received is ', Data
s.send (' Hihi ')
s.close ()
Run Result:
Server
[work@db-testing-com06-vm3.db01.baidu.com python]$ python server.py
Got connected from (' 127.0.0.1 ', 47872)
Hihi
Client
[work@db-testing-com06-vm3.db01.baidu.com python]$ python client.py
The data received is Byebye
==============================================================================
References: HTTP://WWW.CPPBLOG.COM/LAI3D/ARCHIVE/2008/02/1 9/42919.html
A simple example of server and client communication
Server:
Import Socket
s=socket.socket ()
s.bind (' xxx.xxx.xxx.xxx ', xxxx) #ip地址和端口号
S.listen (5)
CS, Address = s.accept ()
print "Got connected from", Address
cs.send (' Byebye ')
ra=cs.recv (r)
Print RA
Cs.close ()