python start http service

    It has not been updated for a long time, and there are some new learning and attempts recently, so let's update it, so that you can remember how to deal with the same problem in the future!

    Recently, I am working on an information recommendation algorithm, which is generally based on content recommendation. It is also possible to implement real-time recommendation, that is, it can capture the user's interest points according to the user's recent browsing, so as to perform similarity recall and sorting according to the user's real-time interests. Algorithms There is nothing to say in terms of using gensim, jieba, hanlp modules

    There are also some pitfalls in the installation of the hanlp module. In fact, you can directly install pyhanlp, then install the JVM, and set the environment variables. I installed it according to the following link at first, and it was so bad that I couldn't install it successfully (http://www.hankcs.com/nlp/python-calls-hanlp.html). Later, I went to the official website of hanlp and found that there is also pyhanlp. Things, use pip install pyhanlp directly to install successfully, pyhanlp installation is successful, jpype is also installed successfully, and then directly from pyhanlp import * seg_list = HanLP.segment(content) when using

    Off topic, this article mainly introduces python to start the http service. Originally, I passed the .py file required for the model and deployment to my colleagues, and then the colleagues packaged the JSF service, and then another colleague called it, which can be realized in real time, but there is a The limitation is that only one model file can be uploaded, and this time, in addition to the tfidf model file, I also have an additional dictionary file, and the launch is imminent, so I plan to apply for multiple servers, enable multiple http services, and use load balancing method to achieve

So how to use python to start http service, this uses python's flask module, Flask is a web micro-framework written in Python, the focus is on the app.run module, I have to thank my colleague Qiu Yujiang ( thinking of colleagues ) I shouldn't have seen the blog, so I just wrote the name of my colleague! I always felt that meeting was fate. Thank you to every colleague for helping me in my work. ) It happened that he wrote the graduation project for his friend's girlfriend and used it in the book. Then send the code directly to me, and help me with super detailed notes on where I need to change, which is great, so I quickly changed my code and realized the python-enabled http service. But there is a pit in this, that is, I don't have any settings for the parameters in app.run(), except for debug = True, run it on my local machine, and then use postman to access it, it's completely fine, but my upstream call can't connect, We racked our brains and thought it was a network and port problem, and finally found out that it was a problem with app.run() (note, discovered by Qiu Yujiang)

app .run ( host = '0.0.0.0' ) so that computers in other networks can access my local http service
The download and installation of postman is also written:

Download postman, it is strange to say that the suffix of the downloaded postman file may be .crx, change the suffix to .zip or .rar to convert it into a compressed file, extract it to a folder, and then use Google-more tools-extension program - Switch to "Developer Mode" - Load the unzipped extension - Select the file directory you unzipped - Click OK, then how to open it? Enter in the Google browser address bar: chrome://apps/ to see the postman chart, you can open it on a single machine

postman sends a json string. Since my input parameters are 4 lists, I used a relatively easy-to-use and flexible json string. I can write the pick-up string and go to https://www.json.cn/ to verify whether it is written correctly. , and then select post-body-raw- to write your json string into the input area, click send, then you will return to your code, you can debug it, and the json string needs to be parsed according to the format


Go back to your own posttest function, you need to parse, the general parsing code is as follows

@app.route('/posttest',methods =['POST'])
def testPost():
error = None
if request.method == 'POST':
data = request.get_data()
j_data = json.loads(data)
j_data_1 = j_data['value']
listt = j_data_1[0]
query = listt['query'                                                ]
        query_id_list = []
for i in range(0,len(query)):
id = query[i]["id"]
query_id_list.append(id)
querycontent = listt['querycontent']
query_content_list = []
for i in range(0, len(querycontent)):
content =                                 
                                    querycontent[i]["content"]
            query_content_list.append(content)
        recall = listt['recall']
        recall_id_list = []
for i in range(0, len(recall)):
id = recall[i]["id"]
recall_id_list.append(id)
recallcontent = listt['recallcontent']
                                
                recall_content_list = []
for i in range(0, len(recallcontent)):
content = recallcontent[i]["content"]
recall_content_list.append(content)
predict_list = predict(query_content_list, query_id_list, recall_content_list, recall_id_list, tfidf,                                
        
                               dictionary)
        prestr = ",".join(predict_list)
return prestr
    else:
return "error"                

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324848521&siteId=291194637