python中使用epoll開發服務端程式

2021-06-21 14:29:22 字數 4359 閱讀 9132

**:

python**:

import socket, logging

import select, errno

logger = logging.getlogger("network-server")

def initlog():

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)

except socket.error, msg:

logger.error("create a socket failed")

try:

listen_fd.setsockopt(socket.sol_socket, socket.so_reuseaddr, 1)

except socket.error, msg:

logger.error("setsocketopt error")

try:

listen_fd.bind(('', 2003))

except socket.error, msg:

logger.error("listen file id bind ip error")

try:

listen_fd.listen(10)

except socket.error, msg:

logger.error(msg)

try:

epoll_fd = select.epoll()

epoll_fd.register(listen_fd.fileno(), select.epollin)

except select.error, msg:

logger.error(msg)

connections = {}

addresses = {}

datalist = {}

while true:

epoll_list = epoll_fd.poll()

for fd, events in epoll_list:

if fd == listen_fd.fileno():

conn, addr = listen_fd.accept()

logger.debug("accept connection from %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

elif select.epollin & events:

datas = ''

while true:

try:

data = connections[fd].recv(10)

if not data and not datas:

epoll_fd.unregister(fd)

connections[fd].close()

logger.debug("%s, %d closed" % (addresses[fd][0], addresses[fd][1]))

break

else:

datas += data

except socket.error, msg:

if msg.errno == errno.eagain:

logger.debug("%s receive %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        

elif select.epollhup & events:

epoll_fd.unregister(fd)

connections[fd].close()

logger.debug("%s, %d closed" % (addresses[fd][0], addresses[fd][1])) 

elif select.epollout & events:

sendlen = 0             

while true:

sendlen += connections[fd].send(datalist[fd][sendlen:])

if sendlen == len(datalist[fd]):

break

epoll_fd.modify(fd, select.epollin | select.epollet)                 

else:

continue

客戶端程式,python**:

import socket

import time

import logging

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)

except socket.error, msg:

logger.error(msg)

try:

connfd.connect(("192.168.31.226", 2003))

logger.debug("connect to network server success")

except socket.error,msg:

logger.error(msg)

for i in range(1, 11):

data = "the number is %d" % i

if connfd.send(data) != len(data):

logger.error("send data to network server failed")

break

readdata = connfd.recv(1024)

print readdata

time.sleep(1)

connfd.close()

這是個很簡單的c/s模型的程式,流程其實和c語言相差不大,就是為了學學python中的基本語法,以及logging,select,socket模組的使用。客戶端傳送字串,服務端再將該字串返回客戶端,epoll中使用的邊緣觸發。

這個有一篇很權威的文章講這個

python中使用epoll開發服務端程式

python import socket,logging import select,errno logger logging.getlogger network server def initlog logger.setlevel logging.debug fh logging.filehand...

epoll監聽檔案 epoll的使用

epoll i o event notification facility 在linux的網路程式設計中,很長的時間都在使用select來做事件觸發。在linux新的核心中,有了一種替換它的機制,就是epoll。相比於select,epoll最大的好處在於它不會隨著監聽fd數目的增長而降低效率。因為...

和 在python中使用

給定兩個大小為 m 和 n 的有序陣列 nums1 和 nums2。請你找出這兩個有序陣列的中位數,並且要求演算法的時間複雜度為 o log m n 你可以假設 nums1 和 nums2 不會同時為空。示例 1 nums1 1 3 nums2 2 則中位數是 2.0 示例 2 nums1 1 2 ...