程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python epoll socket creates a server and accesses it with a local browser

編輯:Python

1、 Environmental Science

        python3 、 ubuntu System (windows Not under the system epoll library , Nor does it support epoll)

2、 Ideas

        1、 Create a listening socket ,

        2、 Binding local IP and port Information , Local information is in tuple format

        3、 Use listen Change monitoring status , Set listening capacity

        4、 establish epoll object , Put the listening socket into epoll in .

        5、 Create an empty dictionary to put epoll Information returned

        6、 Recycle epoll Information returned

        7、 Handle the returned information differently

3、epoll Please refer to for the simple usage of :python Network programming epoll Use _Dream home-CSDN Blog

Code up :

import socket
from socket import *
import re,select
def client_sever(new_socket,request):
# Use regular expressions to match the information sent by the client , Get the information needed by the server , The matching resource address
g=re.match(r'[^/]*/+([^ ]*)\s+',request)
file_name=g.group(1)
# According to the match , Operate locally
# Open local resource , Read the local data as the response body
try:
with open(file_name,'rb') as f:
response_body=f.read()
except:
response_body = '<h1>hello world!</h1>'.encode('utf-8')
finally:
response_header = 'HTTP/1.1\r\n'
response_header += '\r\n'
print(file_name)
# Assemble local data a
response = response_header.encode('utf-8') + response_body
# 8、 Send the matched local resources to the client
new_socket.send(response)
# 9、 Close client socket , End of service , Wait for the next client link .
new_socket.close()
def main():
# 1、 Create a listening socket
server_socket=socket(AF_INET,SOCK_STREAM)
# close socket , Port immediately released
server_socket.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
#server_socket.setblocking(False)
# 2、 Binding local IP and port Information , Local information is in tuple format
server_socket.bind(('127.0.0.1',8080))
# 3、 Use listen Change monitoring status , Set listening capacity
server_socket.listen(128)
# 4、 establish epoll
epl=select.epoll()
# 5、 Put the listening socket into epoll in , The first parameter is the file descriptor ,EPOLLIN Indicates that the corresponding file descriptor can be read
epl.register(server_socket.fileno(),select.EPOLLIN)
# 6、 Create an empty dictionary to put epoll Information returned
fd_event_dict = dict()
fd_event_dict[server_socket.fileno()] = server_socket
while True:
# The default is blocked , until os detected epoll Data in is modified , Back to the event
events = epl.poll()
# Recycle epoll List of returned
for fd ,event in events:
# The returned file descriptor is equal to the file descriptor of the listening socket
if fd == server_socket.fileno():
# Receive the server link , Create a client socket to serve the client
new_socket, client_addr = server_socket.accept()
# Add the socket of the service client epoll in
epl.register(new_socket.fileno(),select.EPOLLIN)
fd_event_dict[new_socket.fileno()] = new_socket
elif event == select.EPOLLIN: # Can read the event
print(' Receiving information ')
new_socket = fd_event_dict[fd]
# Get the information sent by the server , And set the received data byte size
request = new_socket.recv(1024).decode('utf-8')
if request:
client_sever(new_socket,request)
elif event == select.EPOLLHUP: # Customer disconnection event , That is, the client calls close() Method
print(' Close the client service socket ')
# Close the client service socket
fd_event_dict[fd].close()
epl.unregister(fd)
del fd_event_dict[fd]
if __name__ == '__main__':
main()


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved