最近完成了文本相似度的工程,但是又面临python代码和Java平台的对接问题,之前的sklearn2pmml算是一声叹息了。
心烦。
本来用的java runtime,不知道为什么,跟我对接的同事给我说一个python对外提供HTTP服务,抱着学习的心态还是研究一下吧。
# coding:utf-8
import socket
from multiprocessing import Process
HTML_ROOT_DIR = ""
def handle_client(client_socket):
"""处理客户端请求"""
# 获取客户端请求数据
request_data = client_socket.recv(1024)
print("request data:", request_data)
# 构造响应数据
response_start_line = "HTTP/1.1 200 OK\r\n"
response_headers = "Server: My server\r\n"
response_body = "hello it cast"
response = response_start_line + response_headers + "\r\n" + response_body
print("response data:", response)
# 向客户端返回响应数据
client_socket.send(bytes(response, "utf-8"))
# 关闭客户端连接
client_socket.close()
if __name__ == "__main__":
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 8000))
server_socket.listen(120)
while True:
client_socket, client_address = server_socket.accept()
# print("[%s, %s]用户连接上了"%client_addrest[0],client_address[1])
print("[%s, %s]用户连接上了" % client_address)
handle_client_process = Process(target=handle_client, args=(client_socket,))
handle_client_process.start()
client_socket.close()
# 这是我在网上搜到的一个代码
# 在浏览器输入 http://localhost:8000/
# 会出现 hello itcast
这是最终采取的版本
# coding:utf-8
import json
from flask import Flask, request
import text_process
app = Flask(__name__)
MY_URL = '/knowledge/api/v1/'
# hello = '今天天气真好呀'
# not_hello = '为什么今天天气不好呀'
# keystr="服务器上架"
# topN=3
# get
@app.route(MY_URL + 'get/tasks', methods=['GET'])
def get_task():
param = request.args.to_dict()
# print(param) # request.args请求参数
# print(type(param))
# print(param)
keystr = param['keystr']
topN = param['topN']
# return str(text_process.handle_client("工单主题:核心系统数据库服务器上架;", 3))
# in_json = json.dumps(text_process.handle_client("工单主题:核心系统数据库服务器上架;", 3))
# return str(in_json)
# print(text_process.handle_client("工单主题:核心系统数据库服务器上架;", 3))
return str(text_process.handle_client(keystr, topN))
# post
@app.route(MY_URL + 'post/tasks', methods=['POST'])
def post_task():
param = request.json
# print(param) # request.args请求参数
# print(type(param))
# print(param)
keystr = param['keystr']
topN = param['topN']
# return str(text_proprint(text_process.handle_client("工单主题:核心系统数据库服务器上架;", 3))cess.handle_client("工单主题:核心系统数据库服务器上架;", 3))
#
# in_json = json.dumps(text_process.handle_client(keystr, topN))
# return str(in_json)
return str(text_process.handle_client(keystr, topN))
if __name__ == '__main__':
# app.run(debug=True, host='0.0.0.0', port=8000)
app.run(debug=True)
# http://127.0.0.1:5000/knowledge/api/v1/get/tasks?keystr="服务器上架"&topN=3
网友评论