Python Socket

1077 ワード

#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()