# coding:utf-8
import socket
import re
import sys
from multiprocessing import Process
# from TGWebFramework import app
# from Django import app
HTML_ROOT_DIR = "./html"
class HTTPServer(object):
def __init__(self, application):
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.app = application
def start(self):
self.server_socket.listen(128)
while True:
client_socket, client_address = self.server_socket.accept()
print("[%s, %s]用户连接上了" % client_address)
handle_client_process = Process(target=self.handle_client, args=(client_socket,))
handle_client_process.start()
client_socket.close()
def start_response(self, status, headers):
response_headers = "HTTP/1.1 " + status + "\r\n"
for header in headers:
response_headers += "%s: %s\r\n" % header
self.response_headers = response_headers
def handle_client(self, client_socket):
request_data = client_socket.recv(1024)
print("request data:", request_data)
request_lines = request_data.splitlines()
for line in request_lines:
print(line)
request_start_line = request_lines[0]
print(request_start_line.decode("utf-8"))
file_name = re.match(r"\w+ +(/[^ ]*) ", request_start_line.decode("utf-8")).group(1)
method = re.match(r"(\w+) +/[^ ]* ", request_start_line.decode("utf-8")).group(1)
env = {
"PATH_INFO": file_name,
"METHOD": method
}
response_body = self.app(env, self.start_response)
response = self.response_headers + "\r\n" + response_body
client_socket.send(bytes(response, "utf-8"))
client_socket.close()
def bind(self, port):
self.server_socket.bind(("", port))
def main():
if len(sys.argv) < 2:
sys.exit("python TGWebServer.py Module:app")
module_name, app_name = sys.argv[1].split(":")
m = __import__(module_name)
app = getattr(m, app_name)
http_server = HTTPServer(app)
http_server.bind(9999)
http_server.start()
if __name__ == "__main__":
main()
配置启动参数
如果不配置启动参数,直接导入app,则web服务器代码如下:
# coding:utf-8
import socket
import re
import sys
from multiprocessing import Process
# from TGWebFramework import app
from Django import app
HTML_ROOT_DIR = "./html"
class HTTPServer(object):
def __init__(self, application):
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.app = application
def start(self):
self.server_socket.listen(128)
while True:
client_socket, client_address = self.server_socket.accept()
print("[%s, %s]用户连接上了" % client_address)
handle_client_process = Process(target=self.handle_client, args=(client_socket,))
handle_client_process.start()
client_socket.close()
def start_response(self, status, headers):
response_headers = "HTTP/1.1 " + status + "\r\n"
for header in headers:
response_headers += "%s: %s\r\n" % header
self.response_headers = response_headers
def handle_client(self, client_socket):
request_data = client_socket.recv(1024)
print("request data:", request_data)
request_lines = request_data.splitlines()
for line in request_lines:
print(line)
request_start_line = request_lines[0]
print(request_start_line.decode("utf-8"))
file_name = re.match(r"\w+ +(/[^ ]*) ", request_start_line.decode("utf-8")).group(1)
method = re.match(r"(\w+) +/[^ ]* ", request_start_line.decode("utf-8")).group(1)
env = {
"PATH_INFO": file_name,
"METHOD": method
}
response_body = self.app(env, self.start_response)
response = self.response_headers + "\r\n" + response_body
client_socket.send(bytes(response, "utf-8"))
client_socket.close()
def bind(self, port):
self.server_socket.bind(("", port))
def main():
# if len(sys.argv) < 2:
# sys.exit("python TGWebServer.py Module:app")
# module_name, app_name = sys.argv[1].split(":")
# m = __import__(module_name)
# app = getattr(m, app_name)
http_server = HTTPServer(app)
http_server.bind(9999)
http_server.start()
if __name__ == "__main__":
main()
即开始处选择
# from TGWebFramework import app
from Django import app
和结尾处注释
# if len(sys.argv) < 2:
# sys.exit("python TGWebServer.py Module:app")
# module_name, app_name = sys.argv[1].split(":")
# m = __import__(module_name)
# app = getattr(m, app_name)
程序启动后效果如下(在浏览器地址栏处输入http://127.0.0.1:9999/sayhello)
附两者框架处理代码
TGWebFramework.py
# coding:utf-8
import time
HTML_ROOT_DIR = "./html"
class Application(object):
def __init__(self, urls):
self.urls = urls
def __call__(self, env, start_response):
path = env.get("PATH_INFO", "/")
if path.startswith("/static"):
file_name = path[7:]
try:
file = open(HTML_ROOT_DIR + file_name, "rb")
except IOError:
status = "404 Not Found"
headers = []
start_response(status, headers)
return "not found"
else:
file_data = file.read()
file.close()
status = "200 OK"
headers = []
start_response(status, headers)
return file_data.decode("utf-8")
for url, handler in self.urls:
if path == url:
return handler(env, start_response)
status = "404 Not Found"
headers = []
start_response(status, headers)
return "not found"
def show_ctime(env, start_response):
status = "200 OK"
headers = [
("Content-Type", "text/plain")
]
start_response(status, headers)
return time.ctime()
def say_hello(env, start_response):
status = "200 OK"
headers = [
("Content-Type", "text/plain")
]
start_response(status, headers)
return "hello targetcloud"
def say_haha(env, start_response):
status = "200 OK"
headers = [
("Content-Type", "text/plain")
]
start_response(status, headers)
return "hello haha"
urls = [
("/", show_ctime),
("/ctime", show_ctime),
("/sayhello", say_hello),
("/sayhaha", say_haha),
]
app = Application(urls)
Django.py
# coding:utf-8
import time
HTML_ROOT_DIR = "./html"
class Application(object):
def __init__(self, urls):
self.urls = urls
def __call__(self, env, start_response):
path = env.get("PATH_INFO", "/")
if path.startswith("/static"):
file_name = path[7:]
try:
file = open(HTML_ROOT_DIR + file_name, "rb")
except IOError:
status = "404 Not Found"
headers = []
start_response(status, headers)
return "not found"
else:
file_data = file.read()
file.close()
status = "200 OK"
headers = []
start_response(status, headers)
return file_data.decode("utf-8")
for url, handler in self.urls:
if path == url:
return handler(env, start_response)
status = "404 Not Found"
headers = []
start_response(status, headers)
return "not found"
def show_ctime(env, start_response):
status = "200 OK"
headers = [
("Content-Type", "text/plain")
]
start_response(status, headers)
return time.ctime()
def say_hello(env, start_response):
status = "200 OK"
headers = [
("Content-Type", "text/plain")
]
start_response(status, headers)
return "hello django"
def say_haha(env, start_response):
status = "200 OK"
headers = [
("Content-Type", "text/plain")
]
start_response(status, headers)
return "haha django"
urls = [
("/", show_ctime),
("/ctime", show_ctime),
("/sayhello", say_hello),
("/sayhaha", say_haha),
]
app = Application(urls)