python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python使用epoll实现服务端

python使用epoll实现服务端的方法

作者:linux_c_coding_man

今天小编就为大家分享一篇python使用epoll实现服务端的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

如下所示:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import socket
import select

send_data = "hello world!"
send_len = len(send_data)
recv_len = 1024
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
addr = ("0.0.0.0", 8765)
tcp_socket.bind(addr)
tcp_socket.listen(5)
tcp_socket.setblocking(False)
epoll = select.epoll()
'''(边缘触发)select.EPOLLIN | select.EPOLLET'''
epoll.register(tcp_socket.fileno(), select.EPOLLIN)
'''因为epoll返回的触发事件对应的是套接字文件描述符,所以需要在字典中加入对应关系'''
fd_to_socket = {tcp_socket.fileno():tcp_socket}

while True :
 events = epoll.poll(-1)
 for fd, event in events:
  fd_socket = fd_to_socket[fd]
  if fd == tcp_socket.fileno():
   while True:
    try:
     new_socket, new_addr = fd_socket.accept()
    except socket.error as e:
     (errno, err_msg) = e
     print errno
     print err_msg
     if errno == 11:
      break
    print "new accpet:", new_addr
    new_socket.setblocking(False)
    new_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
    epoll.register(new_socket.fileno(), select.EPOLLIN)
    fd_to_socket[new_socket.fileno()] = new_socket
  elif event&select.EPOLLIN:
   recv_datas = []
   recd = 0
   while (recd < recv_len):
    try:
     recv_data = fd_socket.recv(recv_len - recd)
     '''处理读的正常关闭'''
     if recv_data == "":
      print "close socket"
      epoll.unregister(fd)
      fd_to_socket[fd].close()
      del fd_to_socket[fd]
      break
     else:
      recv_datas.append(recv_data)
      recd = recd + len(recv_data)
    '''处理异常关闭(EAGAIN,EINTR)'''
    except socket.error as e:
     (errno, err_msg) = e
     print errno
     print err_msg
     '''因为用的水平触发,EAGAIN我们跳出循环,等待下次触发再读就好了'''
     if errno == 11:
      break
     '''软中断打断了还要继续读'''
     elif errno == 4:
      continue
     '''其它错误我们直接关闭套接字'''
     else:
      print "close socket"
      epoll.unregister(fd)
      fd_to_socket[fd].close()
      del fd_to_socket[fd]
      break
    print repr(recv_datas)
    total_send = 0
    while total_send < send_len:
     sent = fd_socket.send(send_data[total_send:])
     if sent == 0:
      print "close socket"
      epoll.unregister(fd)
      fd_to_socket[fd].close()
      del fd_to_socket[fd]
      break
     else:
      print repr(send_data[total_send:])
      total_send = total_send + sent

其实这里的异常处理我们也可以用

except IOError as e:
  print e.errno
  print e.strerror

以上这篇python使用epoll实现服务端的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文