When using web.py to do HTTP server development, encountered postman can request to the data normally, but the browser cannot request to the data, check the reason after discovery is a cross-domain request problem.
Cross-domain requests, that is, in the browser window, and a server through a "protocol + domain name + port number" Set up a session, to use any of these three attributes of a different source to submit the request, then the browser thinks you are cross-domain, violating the browser's homologous policy. There are three types of cross-domain access restrictions in the response header for cross-domain request specifications:
Access-control-allow-origin: Restrict the sources that allow cross-domain access, such as http://192.168.10.12:8080, note that this only supports * (for all source) numbers or a source, does not support multiple sources, and if you want to implement multiple sources, You can wrap a collection for each request in the collection to determine whether it exists, if it exists, put it in the response header;
Access-control-allow-methods: Limit the types of HTTP methods that allow cross-domain access, multiple comma-separated, such as: POST, GET, Options,put, DELETE
Access-control-allow-headers: Restrict the HTTP headers that allow cross-domain access, including the headers set here, to allow cross-domain access, such as: Content-type, Content-length, accept-encoding, X-csrf-token, Authorization
web.py using Web.header (), you can define the header.
The complete Httpserver code is as follows "ap-httpserver.py":
[Python]View PlainCopy
- #!/usr/bin/env python
- # Encoding:utf-8
- Import Redis
- Import Web
- Import JSON
- Import string
- From time import time
- URLs = (
- ' /qlljx/realtimedata ', ' RealTimeData '
- )
- App = Web.application (URLs, globals ())
- Def getresult ():
- R = Redis. Redis (host=' 127.0.0.1 ', port=6379)
- Result_list = []
- Regionlist = R.hgetall (' regionlist ')
- timestamp = R.hget (' ZHONGGUO_BGP ', ' timestamp ')
- For region in regionlist:
- Value = {' MIP ': Str (regionlist[region]), ' region ': region, \
- ' Inpps ': Int (r.hget (region, ' Inpps ')), ' Outpps ': Int. (R.hget (Region, ' Outpps ')), \
- ' inbps ': Int (r.hget (region, ' inbps ')), ' outbps ': Int. (R.hget (Region, ' outbps ')), \
- ' pktpct ': String.atof (r.hget (Region, ' pktpct ') "), ' bytpct ': String.atof (r.hget (Region, ' bytpct ')}
- Result_list.append (value)
- result = {' timestamp ': timestamp, ' result ': result_list}
- return Json.dumps (Result)
- Class RealTimeData:
- def POST (self):
- data = Web.data ()
- Request_type = str (json.loads (data) [' type '])
- if Request_type = = ' getrealtimedata ':
- result = GetResult ()
- Web.header ("Access-control-allow-origin", "*")
- #web. Header ("Access-control-allow-methods", "POST, GET, OPTIONS, PUT, DELETE")
- #web. Header ("Access-control-allow-headers", "Accept, Content-type, content-length, \
- # accept-encoding, X-csrf-token, Authorization ")
- return result
- if __name__ = = "__main__":
- App.run ()
Only the "Access-control-allow-origin" limit is used, allowing requests from all sources. Start Httpserver:
- [Email protected] python]#/ap-httpserver.py 1216
Using the browser to request data is OK.
Python uses web.py to develop httpserver to address cross-domain issues with post requests