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

时间:2022-06-01 21:30:57

如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/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

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

?
1
2
3
except IOError as e:
  print e.errno
  print e.strerror

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

原文链接:https://blog.csdn.net/peng314899581/article/details/78066087