Python Socket

#coding= UTF-8
import socket
HOST = 'localhost'
PORT = 8097

while True:
    temp = raw_input("请输入:")
    if temp =="exit":
        break
    elif temp!="":
	s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
	strd = temp + ""
	s.connect((HOST,PORT))
	s.send(strd+"")
	data = s.recv(1024)
	print data
	s.close()


#coding= UTF-8  
#pythonsocket通信  
import socket  
import time  

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
sock.bind(('localhost', 8097)) #绑定IP地址和端口号  
sock.listen(5)  
while True:   
     
	thistime=time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime())
	try:          
		connection,address = sock.accept()          
		connection.settimeout(5)#设置超时间          
		buf = connection.recv(1024) #设置接收长度          
		print (thistime+"接收到:"+buf+"")          
		connection.send(thistime+':'+buf)      
	except socket.timeout:          
		print 'time out'      
	connection.close() 


你可能感兴趣的:(socket,python)