Django - 远程主机

时间:2017-12-29 09:33:14

标签: javascript ajax django get httprequest

我遇到了编码问题:
1.我的网页使用参数X向服务器发送请求,参数X是URL
2.服务器获取X并打开与X的新连接并从那里获取数据 3.服务器使用检索到的数据发送对我的网页的响应

我用JS get发送请求。问题是,一旦服务器获取请求并打开与X的新连接,它就会断开与我的网页的连接。

我正在使用Django,据我所知,它只能在给定时间处理一个请求。

内部文件就绪是发送请求:

$("#valid").click(function(){

        $.get("/extract/validate_xpath", {url:document.getElementById('v_url').value,
            query:document.getElementById('v_query').value} ,function(data, status){
            console.log(data);
            console.log(status);
        });

    });

这是假设处理请求的Django视图:

def validate_xpath(request):
  dic=request.GET.copy()
  data = urlopen(dic['url']).read()
  tree = etree.HTML(data)
  response = tree.xpath(dic['query'])
return HttpResponse(dumps(response))

错误发生在

  

的urlopen(DIC [ 'URL'])。读()

我得到的错误:

    [29/Dec/2017 12:06:20] "GET /static/extract/help.js HTTP/1.1" 304 0
8000
http://shut.moreshet.co.il/advancedSearchResult.asp?i=0&nop=20&WholeWord=0&aw=&shut=&rabonim=  //a[@class = "text16bold"]/@href
<class 'str'>
[29/Dec/2017 12:06:27] "POST /extract/validate HTTP/1.1" 200 1660
[29/Dec/2017 12:06:44] "GET /extract/validate_xpath?url=http%3A%2F%2Fshut.moreshet.co.il%2FadvancedSearchResult.asp%3Fi%3D0%26nop%3D20%26WholeWord%3D0%26aw%3D%26shut%3D%26rabonim%3D&query=%2F%2Fa%5B%40class%20%3D%20%22text16bold%22%5D%2F%40href HTTP/1.1" 200 135
Traceback (most recent call last):
  File "C:\Users\user\Anaconda3\lib\wsgiref\handlers.py", line 138, in run
    self.finish_response()
  File "C:\Users\user\Anaconda3\lib\wsgiref\handlers.py", line 180, in finish_response
    self.write(data)
  File "C:\Users\user\Anaconda3\lib\wsgiref\handlers.py", line 274, in write
    self.send_headers()
  File "C:\Users\user\Anaconda3\lib\wsgiref\handlers.py", line 332, in send_headers
    self.send_preamble()
  File "C:\Users\user\Anaconda3\lib\wsgiref\handlers.py", line 252, in send_preamble
    self._write(('HTTP/%s %s\r\n' % (self.http_version,self.status)).encode('iso-8859-1'))
  File "C:\Users\user\Anaconda3\lib\wsgiref\handlers.py", line 453, in _write
    result = self.stdout.write(data)
  File "C:\Users\user\Anaconda3\lib\socketserver.py", line 775, in write
    self._sock.sendall(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
[29/Dec/2017 12:06:44] "GET /extract/validate_xpath?url=http%3A%2F%2Fshut.moreshet.co.il%2FadvancedSearchResult.asp%3Fi%3D0%26nop%3D20%26WholeWord%3D0%26aw%3D%26shut%3D%26rabonim%3D&query=%2F%2Fa%5B%40class%20%3D%20%22text16bold%22%5D%2F%40href HTTP/1.1" 500 59
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 59412)
Traceback (most recent call last):
  File "C:\Users\user\Anaconda3\lib\wsgiref\handlers.py", line 138, in run
    self.finish_response()
  File "C:\Users\user\Anaconda3\lib\wsgiref\handlers.py", line 180, in finish_response
    self.write(data)
  File "C:\Users\user\Anaconda3\lib\wsgiref\handlers.py", line 274, in write
    self.send_headers()
  File "C:\Users\user\Anaconda3\lib\wsgiref\handlers.py", line 332, in send_headers
    self.send_preamble()
  File "C:\Users\user\Anaconda3\lib\wsgiref\handlers.py", line 252, in send_preamble
    self._write(('HTTP/%s %s\r\n' % (self.http_version,self.status)).encode('iso-8859-1'))
  File "C:\Users\user\Anaconda3\lib\wsgiref\handlers.py", line 453, in _write
    result = self.stdout.write(data)
  File "C:\Users\user\Anaconda3\lib\socketserver.py", line 775, in write
    self._sock.sendall(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\user\Anaconda3\lib\wsgiref\handlers.py", line 141, in run
    self.handle_error()
  File "C:\Users\user\Anaconda3\lib\site-packages\django\core\servers\basehttp.py", line 88, in handle_error
    super(ServerHandler, self).handle_error()
  File "C:\Users\user\Anaconda3\lib\wsgiref\handlers.py", line 368, in handle_error
    self.finish_response()
  File "C:\Users\user\Anaconda3\lib\wsgiref\handlers.py", line 180, in finish_response
    self.write(data)
  File "C:\Users\user\Anaconda3\lib\wsgiref\handlers.py", line 274, in write
    self.send_headers()
  File "C:\Users\user\Anaconda3\lib\wsgiref\handlers.py", line 331, in send_headers
    if not self.origin_server or self.client_is_modern():
  File "C:\Users\user\Anaconda3\lib\wsgiref\handlers.py", line 344, in client_is_modern
    return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\user\Anaconda3\lib\socketserver.py", line 639, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Users\user\Anaconda3\lib\socketserver.py", line 361, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\user\Anaconda3\lib\socketserver.py", line 696, in __init__
    self.handle()
  File "C:\Users\user\Anaconda3\lib\site-packages\django\core\servers\basehttp.py", line 155, in handle
    handler.run(self.server.get_app())
  File "C:\Users\user\Anaconda3\lib\wsgiref\handlers.py", line 144, in run
    self.close()
  File "C:\Users\user\Anaconda3\lib\wsgiref\simple_server.py", line 35, in close
    self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
----------------------------------------

使其有效的任何解决方法?

由于

0 个答案:

没有答案