Return different content according to different requests

Return different content based on different requests
Simplify the previous code a bit:

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
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 response is 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.
We can get the address of the request in the first line of the request header, and the data may be empty when the browser requests, add the following code in 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))
You can see that after visiting localhost:8090/login in the browser, the console outputs the access path:

Next, we can use this information to customize different response_body (at this time, the default reading code of the browser is gbk, 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 a 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 are looking for does not exist!!!'
You can access it in the browser and the effect is 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 are looking for 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

phone Contact Us