客户端:
import socket, sys
host = '10.248.27.23'
# host = raw_input("Plz imput destination IP:")
# data = raw_input("Plz imput what you want to submit:")
port = 51423
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
s.connect((host, port))
except socket.gaierror, e:
print "Address-related error connecting to server: %s" %e
sys.exit(1)
except socket.error, e:
print "Connection error: %s" %e
sys.exit(1)
while 1:
try:
data = raw_input("I say: ")
s.send(data)
buf = s.recv(1024)
if len(buf):
print "he say: "+buf
except:
print "Dialogue Over"
s.close()
sys.exit(0)
服务器:
import socket, traceback, sys
host = ''
port = 51423
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(1)
ClientSock, ClientAddr = s.accept()
while 1:
try:
buf = ClientSock.recv(1024)
if len(buf):
print "he say: "+buf
data = raw_input("I say: ")
ClientSock.sendall(data)
except:
print "Dialogue Over"
ClientSock.close()
sys.exit(0)