如何解决“此服务器无法理解的请求”错误?

时间:2019-03-26 09:12:41

标签: python multithreading flask

我已经用Flask构建了一个API,并在用邮递员进行测试时不断出错。

我仅在添加线程时才出现此错误,以便在将数据返回邮递员后可以继续运行我的scraper,现在我不确定如何解决此问题

我的代码如下:

from threading import Thread
from flask import Flask

application = Flask(__name__)

class Compute(Thread):
    def __init__(self, request):
        print("init")
        Thread.__init__(self)
        self.request = request

    def run(self):
        print("RUN")
        command = './webscraper.py -us "{user}" -p "{password}" -url "{url}"'.format(**self.request.json)
        output = subprocess.call(['bash','-c', command])
        print("done")

@application.route('/scraper/run', methods=['POST'])
def init_scrape():
    thread_a = Compute(request.__copy__())
    thread_a.start()
    return jsonify({'Scraping this site: ': request.json["url"]}), 201

if __name__ == '__main__':
    application.run(host="0.0.0.0", port="8080")

我的POST数据只是一个网站url和登录它的详细信息,看起来像这样

data = {

{
    "user":"username",
    "password":"password",
    "url":"www.mysite.com/"

}

如果我与邮递员向localhost:8080/scraper/run发送POST请求,则会出现此错误:

init
RUN
Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "api_app.py", line 19, in run
    command = './portal_scrape.py -us "{user}" -p "{password}" -start {start}  -end {end} -fav "{favourite}" -url "{url}"'.format(**self.request.json)
  File "/home/connor/Desktop/portal_dl/venv36/lib/python3.6/site-packages/flask/wrappers.py", line 47, in json
    return self.get_json()
  File "/home/connor/Desktop/portal_dl/venv36/lib/python3.6/site-packages/flask/wrappers.py", line 71, in get_json
    data = self._get_data_for_json(cache=cache)
  File "/home/connor/Desktop/portal_dl/venv36/lib/python3.6/site-packages/flask/wrappers.py", line 50, in _get_data_for_json
    return self.get_data(cache=cache)
  File "/home/connor/Desktop/portal_dl/venv36/lib/python3.6/site-packages/werkzeug/wrappers.py", line 514, in get_data
    rv = self.stream.read()
  File "/home/connor/Desktop/portal_dl/venv36/lib/python3.6/site-packages/werkzeug/wsgi.py", line 1307, in read
    return self.on_disconnect()
  File "/home/connor/Desktop/portal_dl/venv36/lib/python3.6/site-packages/werkzeug/wsgi.py", line 1275, in on_disconnect
    raise ClientDisconnected()
werkzeug.exceptions.ClientDisconnected: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.


我正在发送与它在没有线程的情况下使用的相同的POST请求

0 个答案:

没有答案