1. 程式人生 > >python 接收藍芽閘道器http請求失敗 瀏覽器卻成功(已解決)

python 接收藍芽閘道器http請求失敗 瀏覽器卻成功(已解決)

完全可以測通的(是因為防火牆的問題) 

原因:開啟了防火牆在本地是可以測通的  外面的機器是訪問不了我的電腦的

解決辦法:關閉防火牆就可以了

第一種方法

from http.server import BaseHTTPRequestHandler
from http.server import HTTPServer
from socketserver import ThreadingMixIn
 
hostIP = '10.33.4.31'
portNum = 1883
class mySoapServer( BaseHTTPRequestHandler ):
    print('mySoapServer')
    def do_head( self ):
        print('do_head')
        pass
   
    def do_GET( self ):
        print('do_GET')
        try:
            self.send_response( 200, message = None )
            
            self.send_header( 'Content-type', 'text/html' )
            self.end_headers()
            res = '''
           <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
           <HTML>
           <HEAD><META content="IE=5.0000" http-equiv="X-UA-Compatible">
           <META content="text/html; charset=gb2312" http-equiv=Content-Type>
           </HEAD>
           <BODY>
           Hi, www.perlcn.com is a good site to learn python!
           </BODY>
           </HTML>
           '''
            self.wfile.write( res.encode( encoding = 'utf_8', errors = 'strict' ) )
        except IOError:
            self.send_error( 404, message = None )
   
    def do_POST( self ):
        print('do_POST')
        try:
            self.send_response( 200, message = None )
            
            self.send_header( 'Content-type', 'text/html' )
            self.end_headers()
            res = '''
           <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
           <HTML>
           <HEAD><META content="IE=5.0000" http-equiv="X-UA-Compatible">
           <META content="text/html; charset=gb2312" http-equiv=Content-Type>
           </HEAD>
           <BODY>
           Hi, www.perlcn.com is a good site to learn python!
           </BODY>
           </HTML>
           '''
            self.wfile.write( res.encode( encoding = 'utf_8', errors = 'strict' ) )
        except IOError:
            self.send_error( 404, message = None )
 
class ThreadingHttpServer( ThreadingMixIn, HTTPServer ):
    pass
     
myServer = ThreadingHttpServer( ( hostIP, portNum ), mySoapServer )
myServer.serve_forever()
myServer.server_close()

輸出:

mySoapServer
do_GET
10.33.4.31 - - [28/Dec/2018 13:42:59] "GET /myapps HTTP/1.1" 200 -
do_GET
10.33.4.31 - - [28/Dec/2018 13:43:00] "GET /myapps HTTP/1.1" 200 -
10.33.4.31 - - [28/Dec/2018 13:43:24] "GET /myapps HTTP/1.1" 200 -
do_GET
10.33.4.31 - - [28/Dec/2018 13:43:25] "GET /myapps HTTP/1.1" 200 -
do_GET

 

第二種方法

import tornado.ioloop
import tornado.web
import msgpack

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, get")
        print('get')
    def post(self):
        package=self.request.files['chunk'][0]['body'];
        #print(payloads);
        print('post')
        print(msgpack.unpackb(package,use_list=False, raw=False));
    
application = tornado.web.Application([
    (r"/", MainHandler),])
 
if __name__ == "__main__":
    application.listen(1883,'10.33.4.31')
    tornado.ioloop.IOLoop.instance().start()