#coding:utf-8importsignal,time,sys,threading,os.path,os,subprocess,socket,selectdefstart():try:sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)sock.bind(('localhost'...
#coding: utf-8
import signal,time,sys,threading,os.path,os,subprocess,socket,select
def start():
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost', 8800))
sock.listen(5)
except Exception as e:
print 'fail'
exit(0)
while True:
clientsock,clientaddr = sock.accept()
sock.setblocking(0)#是不是只要有这句就是实现非阻塞呀,还是要用select模块
buf = clientsock.recv(1024)
clientsock.send(buf)
clientsock.close()
if __name__ == "__main__":
start()
客户端代码:
def client():
while(1):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 8800))
print u'请输入要发送的字符串:'
k = raw_input()
sock.send(k)
print sock.recv(1024)
sock.close()
if __name__ == "__main__":
client()
展开