Python: The use of thread locks and thread safety
リクエストに応じて異なるコンテンツを返す
前述のコードを少し簡略化します。
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'))
このコードの実行中、レスポンスは hello world になります。次に、リクエストに応じて異なるコンテンツを返すようにし、ブラウザが /register や /login にアクセスしたときにそれぞれ異なるコンテンツを返せるようにします。
リクエストヘッダーの 1 行目からリクエストのパスを取得できます。ブラウザからのリクエストではデータが空の場合があるため、ループ内に次のコードを追加します。
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 にアクセスすると、コンソールにアクセスパスが出力されることが確認できます。
次に、この情報を使って異なる response_body をカスタマイズできます(このとき、ブラウザのデフォルト読み取りエンコーディングは GBK であるため、コード側で調整が必要です)。
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'
UTF-8 エンコーディングで返したい場合は、レスポンスヘッダーの content-type:text/html を content-type:text/html;charset=utf8 に書き換えて、ブラウザに対応コンテンツのエンコーディング方式を通知するだけで済みます。
ブラウザで何にもアクセスしない場合でも、対応するレスポンスコンテンツが必要です。分岐文に次の分岐を追加します。
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。。。'
ブラウザでアクセスして、次のような効果を確認できます。
オブジェクト指向によるサーバーのカプセル化
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()
前述のコードを少し簡略化します。
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'))
このコードの実行中、レスポンスは hello world になります。次に、リクエストに応じて異なるコンテンツを返すようにし、ブラウザが /register や /login にアクセスしたときにそれぞれ異なるコンテンツを返せるようにします。
リクエストヘッダーの 1 行目からリクエストのパスを取得できます。ブラウザからのリクエストではデータが空の場合があるため、ループ内に次のコードを追加します。
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 にアクセスすると、コンソールにアクセスパスが出力されることが確認できます。
次に、この情報を使って異なる response_body をカスタマイズできます(このとき、ブラウザのデフォルト読み取りエンコーディングは GBK であるため、コード側で調整が必要です)。
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'
UTF-8 エンコーディングで返したい場合は、レスポンスヘッダーの content-type:text/html を content-type:text/html;charset=utf8 に書き換えて、ブラウザに対応コンテンツのエンコーディング方式を通知するだけで済みます。
ブラウザで何にもアクセスしない場合でも、対応するレスポンスコンテンツが必要です。分岐文に次の分岐を追加します。
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。。。'
ブラウザでアクセスして、次のような効果を確認できます。
オブジェクト指向によるサーバーのカプセル化
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
-
A detailed explanation of Hadoop core architecture HDFS
Knowledge Base Team
-
What Does IOT Mean
Knowledge Base Team
-
6 Optional Technologies for Data Storage
Knowledge Base Team
-
What Is Blockchain Technology
Knowledge Base Team
Explore More Special Offers
-
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
