Python Socket

来源:互联网 发布:淘宝客服销售案例班牛 编辑:程序博客网 时间:2024/06/03 14:59
#coding= UTF-8import socketHOST = 'localhost'PORT = 8097while 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 datas.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() 


0 0