在服务器上的代码如下
from __future__ import unicode_literals
from flask_restful import Api
from flask import Flask,request
import time
import datetime
#########product==========
import json
#####放在11.46库
import TextDropRepeat as tdr
import pymongo
import numpy as np
from numpy import array
app = Flask(__name__)
app.debug = True
app.config.update(RESTFUL_JSON=dict(ensure_ascii=False))
api = Api(app)
TODOS = {
'conhashGroup': {'task': []},
'publishtime': {'task':[]},
'conhashGroupA': {'task': []},
'conhashGroupB': {'task': []},
'conhashGroupC': {'task': []},
'conhashGroupD': {'task': []},
}
from flask_restful import reqparse
parser = reqparse.RequestParser()
parser.add_argument('task')
@app.route('/')
def hello_world():
return 'hello world'
@app.route('/drop_TextRepeat/', methods=['POST'])
def add_task():
print (TODOS)
nowTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
pastTime = (datetime.datetime.now()-datetime.timedelta(days=5)).strftime('%Y-%m-%d %H:%M:%S')
time_start=time.time()
print ('开始获取数据')
####获取数据
try:
data1 = request.form.get('data')
print (1)
except:
data1 = request.data
print (2)
#print (data1)
print ('获取数据成功')
for line in data1.split('\n'):
if 'DRETITLE' in line:
title = line.split('#DRETITLE')[1].strip()
if 'PUBLISHDATE' in line:
publishdate = line.replace('=','').replace('"','').split('PUBLISHDATE')[1].strip().replace("'","")
print (publishdate)
print (title)
###转换64位hash
ConHash = tdr.simhash(title)
####切割4*16hash
ConHashA = str(ConHash)[0:16]
ConHashB = str(ConHash)[16:32]
ConHashC = str(ConHash)[32:48]
ConHashD = str(ConHash)[48:]
if ConHashA in TODOS['conhashGroupA']['task']:
print (3)
TODOS['conhashGroup']['task'].append(ConHash)
TODOS['publishtime']['task'].append(publishdate)
TODOS['conhashGroupA']['task'].append(ConHashA)
TODOS['conhashGroupB']['task'].append(ConHashB)
TODOS['conhashGroupC']['task'].append(ConHashC)
TODOS['conhashGroupD']['task'].append(ConHashD)
index = np.where((pastTime<=array(TODOS['publishtime']['task'])) & (array(TODOS['publishtime']['task'])<nowTime ))[0].tolist()
TODOS['publishtime']['task'] = np.array(TODOS['publishtime']['task'])[index].tolist()
TODOS['conhashGroup']['task'] = np.array(TODOS['conhashGroup']['task'])[index].tolist()
TODOS['conhashGroupA']['task'] = np.array(TODOS['conhashGroupA']['task'])[index].tolist()
TODOS['conhashGroupB']['task'] = np.array(TODOS['conhashGroupB']['task'])[index].tolist()
TODOS['conhashGroupC']['task'] = np.array(TODOS['conhashGroupC']['task'])[index].tolist()
TODOS['conhashGroupD']['task'] = np.array(TODOS['conhashGroupD']['task'])[index].tolist()
time_end=time.time()
print ('totally cost',time_end-time_start)
return 'ok'
if __name__ == '__main__':
app.run(host = '0.0.0.0')
uwsgi配置文件如下
[uwsgi]
socket=/tmp/uwsgi.sock
mount=/myapp=/home/nlp/pyfoot/TD/DropText.py
manage-script-name=true
master=true
processes=4
threads=2
stats=127.0.0.1:9191
callable=app
daemonize = /home/nlp/pyfoot/TD/DropText.log
nginx配置如下
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 8000;
access_log /home/nlp/pyfoot/TD/DR.log main;
server_name 172.22.11.46;
location /myapp {
root /myapp;
include uwsgi_params;
#uwsgi_param SCRIPT_NAME/myapp;
uwsgi_pass unix:/tmp/uwsgi.sock;
}
}
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
}
当我在本地电脑浏览器访问服务器时如下,正常显示helloworld
但是通过py脚本post传输数据时就报错,代码如下
import requests
content = {'data':'#DRETITLE 李克强总理访欧成果惠及企业及民众\n#DREFIELD PUBLISHDATE="2018-07-28 04:42:55"'}
r = requests.post('http://172.22.11.46:8000/myapp/drop_TextRepeat/', data=content)
报错信息如下
python返回值如下
r.text
Out[29]: '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n<title>404 Not Found</title>\n<h1>Not Found</h1>\n<p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>\n'
请问大佬,这个是什么情况,浏览器中可以访问成功,说明配置没啥问题,但是换post接口就报错,小弟一直想不明白,还请大佬解惑
uwsgi文件 的路径 配置错了 具体大家可百度 uwsgi参数设置