The HTTP protocol:


Def handleClient(connfd) def handleClient(connfd): Request = connfd. Recv (4096) # print (" * * * * * * * * * * * ") # # print (request) print (" * * * * * * * * * * * * ") request request_lines = # according to the line cutting request.splitlines() for line in request_lines: print(line.decode()) try: f = open('index.html') except IOError: Response = "HTTP / 1.1 303 Not Found \ r \ n" response + = "\ r \ n" # empty line response + = "' * * * * * * * * * * * * * * * * * * * * * * * * * * Sorry, not found the page. ************************** ''' else: Response = "HTTP/1.1 200 OK\r\n" response += '\r\n' response += f.read() finally: Send (response.encode()) def main(): Sockfd = socket() sockfd.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) sockfd.bind(('0.0.0.0', 8000)) sockfd.listen() while True: print("Listen the port 8000..." HandleClient (connfd) connfd.close() if __name__ == "__main__": main()Copy the code



S.splitlines  
Split file strings are separated by lines






What is IO?

  • It consists of two parts: IO device and IO interface
  • For Linux, I/O operations can be performed in a variety of ways
  • Such as DIO (DirectI/O)
  • AIO (AsynchronousI/O asynchronous I/O)
  • Memory-mappedi /O, etc…
  • Different I/O modes have different implementation modes and performance. You can select different I/O modes for different applications.



In the memory
Data interchange
Can be
The operation is I/O
Such as:
disk
terminal
network



IO secret set type sequence:
Lots of IO operations
Consume less CPU
Long running time
CPU (computing) intensive program:
IO operations
less
CPU consumption big



IO classification:
Blocking I/o:
In the operation of the
IO condition not reached
Or transmission
The slower
A blocking state occurs
Blocking IO
Is the default
Efficiency is very low
Blocking conditions:
Blocking function
File to read and write
Data sent



Non-blocking IO:
Meet the IO
Don’t let it block
implementation
Change the IO
attribute
Become non-blocking
It’s usually going to loop
for
Circulation monitoring



3.IO multiplexing
Definition:
Monitors multiple I/O events at the same time
Execute the I/O event that is ready
IO ready:
The critical state when an IO event is about to occur
irreversible
Create the monitoring
events
judge
The event
To deal with






Function:
Set to non-blocking
Parameters:
False: non-blocking



Timeout detection:
will
blocking
Set up the
Waiting time
Within specified time
Reach the conditions
Normal execution
If the condition is not met, the block ends
S.s ettimeout (SEC)
Function:
timeout
Parameters:
seconds






select
poll   
epoll 



Select (rlist, wlist, xlist[, timeout])
Function:
Monitoring I/O Events
Parameters:
rlist   
Waiting to be processed
wlist   
Take the initiative to deal with
xlist   
Errors are expected to be handled
timeout
Timeout detection
The return value:
rs
IO ready to go
ws
xs



Do not create an infinite loop when processing IO
Will make
The client owns the server



IO multipath revival
Multiple IO effects can be processed simultaneously
High efficiency






Operation:
Bit operations are performed in terms of binary bits
&
 |  
^



<<
>>






Use:
Low-level hardware
Operating register
Flag bit filtering









Poll method for IO multiplexing:
1. Create poll objects:
   p = select.poll
2. Sign up to follow
P.r egister (s, POLLIN | PLLERR)
Don’t focus on:
p.unregister(s)
Event Category:
          POLLIN  POLLOUT  POLLERR  POLLHUP   POLLPRI
Rlist wLIST xlist Disconnect emergency processing
3. Monitor IO:
Events = p.poll ()
monitoring
The event
The return value:
Return to happen
The event
Events is a list [(Fileno, evnet), (), ()….]
each
Corresponds to a tuple
The descriptor
The ready event
IO map
{s.fileno():s}
4. Process I/O events



Select IO multiplexing server side:





From socket import * from select import select # create socket s = socket() # set port reuse Rlist = [s] wList = [] xList = [s] rList = [s] wList = [] xList = [s] Print (" wait IO happens ") rs and ws, xs = select (rlist wlist, xlist) # loop through rs ready list for r in the rs: # if there is a new client link request if r is s: Addr = r.acept () # r==s Print ("Connect from", Rlist. append(connfd) # add client socket to listener list else: Data = r.rv (1024) if not data: Connfd rlist.remove(r) r.close() # close socket else: Print ("Receive:", data.decode()) print("Receive:", data.decode()) # wlist wlist.append(r) # wlist Wlist. remove(w) # xs list: for x in xs: if x is s: s.close()Copy the code





































Client:





Sockfd = socket() # connect(('127.0.0.1', 8888)) while True: MSG = input(" MSG >>") if not: break sockfd.sendall(msg.encode()) data = sockfd.recv(1024) print(data.decode()) sockfd.close()Copy the code



Application:
Select the server, with an eye on the client connection
Client send and terminal input. Write all the client sent and terminal input into one file





# myserver.py # application: # select server, also pay attention to client connection # client send and terminal input. From socket import * from select import * from sys import stdin sock = socket() Sock. setsockopt (SOL_SOCKET, SO_REUSEADDR, 1) sock.bind(("127.0.0.1", 6666)) sock.listen(5) rlist = [sock, stdin] wlist = [] xlist = [] file = open("selectdemo.txt", "w+b") while True: Rs, ws, xs, = select(rlist, wlist, xlist) for rin rs: if r == sock: connfd, addr = r.acept () print("......") ) rlist.append(connfd) elif r == stdin: data = stdin.readline() file.write(data.encode()) file.flush() else: data = r.recv(4096) if not data: rlist.remove(r) r.close() else: File. Write (data + "encode ".encode()) file. Flush () print(" select>>>.txt ", data.decode()) r.end (" .encode()) file.close()Copy the code





From socket import * # create socket sockfd = socket() # connect sockfd.connect(('127.0.0.1', 6666)) while True: MSG = input(" MSG >>") if not: break sockfd.sendall(msg.encode()) data = sockfd.recv(1024) print(data.decode()) sockfd.close()Copy the code