为什么Python的HTTP服务器引起的Andr​​oid VolleyError [英] Why Python http server cause android VolleyError

查看:128
本文介绍了为什么Python的HTTP服务器引起的Andr​​oid VolleyError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简单的Python脚本3.4:

I have following simple Python 3.4 script:

LISTEN_PORT = 7000

class MyHandler(http.server.SimpleHTTPRequestHandler):

    def do_GET(self):
        print("DO GET")
        self.send_response(200, "OK")



def run():

    handler = MyHandler
    print("Server Started")
    httpd = socketserver.TCPServer(("0.0.0.0", LISTEN_PORT), handler)

    try:
        print("serving at port", LISTEN_PORT)
        httpd.serve_forever()
    except KeyboardInterrupt:
        httpd.socket.close()


run()

当我在Android拨打:

When I call from android:

final StringRequest request = new StringRequest(Request.Method.GET, context.getString(R.string.domain)+"/", new Response.Listener<String>() {
        @Override
        public void onResponse(String s) {

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {

            Log.e(TAG, "Can not send token" + volleyError);
            Toast.makeText(context, "Can not send", Toast.LENGTH_LONG).show();
        }
    });

    Volley.newRequestQueue(context).add(request);

Android的:
我看吐司无法发送和logcat的控制台上我有以下信息:

Android: I see Toast "Can not send" and on logcat console I have following information:

06-27 18:05:16.332  28946-28946/com.example.code E/NETWORK_CONNECTOR﹕ Can not send token Code:com.android.volley.NoConnectionError: java.io.EOFException

的Python:

192.168.12.246 - - [27/Jun/2014 18:15:55] "GET / HTTP/1.1" 200 -
DO GET

什么是错的Python脚本?

What is wrong with Python script?

推荐答案

什么是错的python脚本?

What is wrong with python script ?

它会以某种方式与python3.0-3.2工作,但不会与python3.3工作+

it would somehow work with python3.0-3.2 but won't work with python3.3+

从<一个href=\"https://docs.python.org/3/library/http.server.html#http.server.BaseHTT$p$pquestHandler.send_response\"相对=nofollow> send_response文档

在变更3.3版:头被存储到内部缓冲区和end_headers()需要显式调用

Changed in version 3.3: Headers are stored to an internal buffer and end_headers() needs to be called explicitly.

在当前设置HTTP应答的头部部分是根本无法发送。

in the current setup the header part of http answer is simply not sent.

这样做的python3.3 +正确的方法是

so the right way to do it in python3.3+ is

def do_GET(self):

    print("DO GET")
    self.send_response(200, "OK")
    self.end_headers()

    self.wfile.write(b"Response body\n") #optional

但它可以在你的脚本来改善一些东西。

but there is several thing which could be improved in your script


  • 使用http.server.BaseHTT prequestHandler代替http.server.SimpleHTT prequestHandler因为SimpleHTT prequestHandler添加一些魔法BaseHTT prequestHandler处理GET和 HEAD 请求。如你覆盖do_GET,更换SimpleHTT prequestHandler的行为,但由于您没有为do_HEAD做到这一点,你的服务器会回答HEAD请求,因为这将是SimpleHTT prequestHandler这是不是一个好主意

  • httpd.server_close(),而不是httpd.socket.close(),即使不改变的东西太多,因为它基本上是在当前的Python一个空操作

  • use http.server.BaseHTTPRequestHandler instead of http.server.SimpleHTTPRequestHandler because SimpleHTTPRequestHandler add some magic to BaseHTTPRequestHandler to process GET and HEAD request. as you override do_GET, you replace the behavior of SimpleHTTPRequestHandler but as you don't do it for do_HEAD, your server would answer to HEAD requests as it would be SimpleHTTPRequestHandler which is not a good idea
  • httpd.server_close() instead httpd.socket.close() even if that don't change thing too much as it is basically a noop in the current python

在脚本的顶部添加进口瞧一个完整的工作脚本

adding an import at top of the script and voilà a full working script

import http.server
LISTEN_PORT = 7000

class MyHandler(http.server.BaseHTTPRequestHandler):

    def do_GET(self):

        print("DO GET")
        self.send_response(200, "OK")
        self.end_headers()

        self.wfile.write(b"Response body\n") #optional


def run():

    handler = MyHandler
    print("Server Started")
    httpd = http.server.HTTPServer(("0.0.0.0", LISTEN_PORT), handler)

    try:
        print("serving at port", LISTEN_PORT)
        httpd.serve_forever()
    except KeyboardInterrupt:
        httpd.server_close()


if __name__ == "__main__":
    run()

这篇关于为什么Python的HTTP服务器引起的Andr​​oid VolleyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆