|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
小知识:CentOS其实就是相当于免费版的RedHat,任何人可以自由使用,不需要向RedHat付任何的费用。当然,同样你也得不到任何有偿的技术支持和升级服务。
Python代码:
importsocket,logging
importselect,errno
logger=logging.getLogger("network-server")
defInitLog():
logger.setLevel(logging.DEBUG)
fh=logging.FileHandler("network-server.log")
fh.setLevel(logging.DEBUG)
ch=logging.StreamHandler()
ch.setLevel(logging.ERROR)
formatter=logging.Formatter("%(asctime)s-%(name)s-%(levelname)s-%(message)s")
ch.setFormatter(formatter)
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
if__name__=="__main__":
InitLog()
try:
listen_fd=socket.socket(socket.AF_INET,socket.SOCK_STREAM,0)
exceptsocket.error,msg:
logger.error("createasocketfailed")
try:
listen_fd.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
exceptsocket.error,msg:
logger.error("setsocketopterror")
try:
listen_fd.bind((,2003))
exceptsocket.error,msg:
logger.error("listenfileidbindiperror")
try:
listen_fd.listen(10)
exceptsocket.error,msg:
logger.error(msg)
try:
epoll_fd=select.epoll()
epoll_fd.register(listen_fd.fileno(),select.EPOLLIN)
exceptselect.error,msg:
logger.error(msg)
connections={}
addresses={}
datalist={}
whileTrue:
epoll_list=epoll_fd.poll()
forfd,eventsinepoll_list:
iffd==listen_fd.fileno():
conn,addr=listen_fd.accept()
logger.debug("acceptconnectionfrom%s,%d,fd=%d"%(addr[0],addr[1],conn.fileno()))
conn.setblocking(0)
epoll_fd.register(conn.fileno(),select.EPOLLIN|select.EPOLLET)
connections[conn.fileno()]=conn
addresses[conn.fileno()]=addr
elifselect.EPOLLIN&events:
datas=
whileTrue:
try:
data=connections[fd].recv(10)
ifnotdataandnotdatas:
epoll_fd.unregister(fd)
connections[fd].close()
logger.debug("%s,%dclosed"%(addresses[fd][0],addresses[fd][1]))
break
else:
datas+=data
exceptsocket.error,msg:
ifmsg.errno==errno.EAGAIN:
logger.debug("%sreceive%s"%(fd,datas))
datalist[fd]=datas
epoll_fd.modify(fd,select.EPOLLET|select.EPOLLOUT)
break
else:
epoll_fd.unregister(fd)
connections[fd].close()
logger.error(msg)
break
elifselect.EPOLLHUP&events:
epoll_fd.unregister(fd)
connections[fd].close()
logger.debug("%s,%dclosed"%(addresses[fd][0],addresses[fd][1]))
elifselect.EPOLLOUT&events:
sendLen=0
whileTrue:
sendLen+=connections[fd].send(datalist[fd][sendLen:])
ifsendLen==len(datalist[fd]):
break
epoll_fd.modify(fd,select.EPOLLIN|select.EPOLLET)
else:
continue
客户端步伐,Python代码:
importsocket
importtime
importlogging
logger=logging.getLogger("network-client")
logger.setLevel(logging.DEBUG)
fh=logging.FileHandler("network-client.log")
fh.setLevel(logging.DEBUG)
ch=logging.StreamHandler()
ch.setLevel(logging.ERROR)
formatter=logging.Formatter("%(asctime)s-%(name)s-%(levelname)s-%(message)s")
ch.setFormatter(formatter)
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
if__name__=="__main__":
try:
connFd=socket.socket(socket.AF_INET,socket.SOCK_STREAM,0)
exceptsocket.error,msg:
logger.error(msg)
try:
connFd.connect(("192.168.31.226",2003))
logger.debug("connecttonetworkserversuccess")
exceptsocket.error,msg:
logger.error(msg)
foriinrange(1,11):
data="TheNumberis%d"%i
ifconnFd.send(data)!=len(data):
logger.error("senddatatonetworkserverfailed")
break
readData=connFd.recv(1024)
printreadData
time.sleep(1)
connFd.close()
这是个很复杂的C/S模子的步伐,流程实在和C言语相差不年夜,就是为了学学python中的基础语法,和logging,select,socket模块的利用。客户端发送字符串,办事端再将该字符串前往客户端,epoll中利用的边沿触发。
这个有一篇很威望的文章讲这个
http://scotdoyle.com/python-epoll-howto.html
小知识:在过去的四年中,我经常使用CentOS,主要是作为企业Linux商业版本产品的一种替代,很多个人、企业或机构都希望拥有主流企业Linux的稳定性、可靠性和声誉。 |
|