Return Different Content According to Different Requests

Return Different Content According to Different Requests

This section realizes that different content is returned according to different requests, so that the browser accesses /register or /login to return different content.

Return different content according to different requests


Simplify the previous code:
import socket

server_socket = socket.socket ( socket.AF_INET , socket.SOCK_STREAM ) _ _ _ _
server_socket.bind(('0.0.0.0', 8090))
server_socket.listen(128)

while True:
client_socket, client_addr = server_socket.accept()
data = client_socket.recv(1024).decode('utf8')

# 响应头
response_header = 'HTTP/1.1 200 OK ' + ' content-type:text /html '
response_header += ' '

#response body
response_body = 'hello world'

# response
response = response_header + response_body

# send to client
client_ socket.send ( response.encode ( ' utf8' ) )
When this code is running, the responses obtained are all hello world. Now we realize that different content is returned according to different requests, so that the browser can access /register or /login to return different content.
Regarding the requested address, we can get it in the first line of the request header, and when the browser requests it, the data may be empty. Add the following code to the loop body:
path = ''
if data: # # The data sent by the browser may be empty
path = data.splitlines ()[ 0 ].split( ' ' )[ 1 ]
print ( 'The requested path is {}' . format ( path ))
localhost:8090/login in the browser, the console outputs the access path:

Next, we can use this information to customize different response_body (the default reading code of the browser is gbk at this time , which needs to be adjusted in the code):
if path == '/login' :
response_body = 'Welcome to the login page'
elif path == '/register' :
response_body = 'Welcome to the registration page'
elif path == '/' :
response_body = 'Welcome to the homepage'
If we want to return in utf8 encoding, we only need to rewrite content-type: text / htmln in the response header to content-type: text / html; charset = utf8n to inform the browser of the encoding method of the corresponding content.
When nothing is accessed in the browser , there should also be corresponding response content, we can add the following branch to the branch statement:
else :
# Page not found 404 Page Not Found
response_header = 'HTTP/1.1 404 Page Not Found '
response_body = 'Sorry, the page you want to view does not exist!!!'
You can browse it to access the effects as follows:

Object-Oriented Server Encapsulation
import socket


class MyServer ( object ):
def __init _ _ ( self , ip , port ) :
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.bind((ip, port))
self.socket.listen(128)

def run_forever(self):
while True:
client_socket, client_addr = self.socket.accept()
data = client_socket.recv(1024).decode('utf8')

path = ''
if data:
path = data.splitlines()[0].split(' ')[1]

response_header = 'HTTP/1.1 200 OK '

if path == '/login' :
response_body = 'Welcome to the login page'
elif path == '/register' :
response_body = 'Welcome to the registration page'
elif path == '/' :
response_body = 'Welcome to the homepage'
else:
response_header = 'HTTP/1.1 404 Page Not Found '
response_body = 'Sorry, the page you want to view does not exist!!!'

response_header += ' content-type:text / html;charset =utf8 '
response_header += ' '

response = response_header + response_body
client_socket.send(response.encode('utf8'))


server = MyServer('0.0.0.0', 9090)
server.run_forever()




Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00