java - Why Python http server cause android VolleyError -
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 @ port", listen_port) httpd.serve_forever() except keyboardinterrupt: httpd.socket.close() run()
when call 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: see toast
"can not send" , on logcat console 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 -
what wrong python script?
what wrong python script ?
it somehow work python3.0-3.2 won't work python3.3+
from send_response documentation
changed in version 3.3: headers stored internal buffer , end_headers() needs called explicitly.
in current setup header part of http answer not sent.
so right way in python3.3+
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 several thing improved in script
- use http.server.basehttprequesthandler instead of http.server.simplehttprequesthandler because simplehttprequesthandler add magic basehttprequesthandler process , head request. override do_get, replace behavior of simplehttprequesthandler don't do_head, server answer head requests simplehttprequesthandler not idea
- httpd.server_close() instead httpd.socket.close() if don't change thing noop in current python
adding import @ top of script , voilĂ 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 @ port", listen_port) httpd.serve_forever() except keyboardinterrupt: httpd.server_close() if __name__ == "__main__": run()
Comments
Post a Comment