Python uses web.py to develop httpserver to address cross-domain issues with post requests

Source: Internet
Author: User

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
  1. #!/usr/bin/env python
  2. # Encoding:utf-8
  3. Import Redis
  4. Import Web
  5. Import JSON
  6. Import string
  7. From time import time
  8. URLs = (
  9. ' /qlljx/realtimedata ', ' RealTimeData '
  10. )
  11. App = Web.application (URLs, globals ())
  12. Def getresult ():
  13. R = Redis. Redis (host=' 127.0.0.1 ', port=6379)
  14. Result_list = []
  15. Regionlist = R.hgetall (' regionlist ')
  16. timestamp = R.hget (' ZHONGGUO_BGP ', ' timestamp ')
  17. For region in regionlist:
  18. Value = {' MIP ': Str (regionlist[region]), ' region ': region, \
  19. ' Inpps ': Int (r.hget (region, ' Inpps ')), ' Outpps ': Int. (R.hget (Region, ' Outpps ')), \
  20. ' inbps ': Int (r.hget (region, ' inbps ')), ' outbps ': Int. (R.hget (Region, ' outbps ')), \
  21. ' pktpct ': String.atof (r.hget (Region, ' pktpct ') "), ' bytpct ': String.atof (r.hget (Region, ' bytpct ')}
  22. Result_list.append (value)
  23. result = {' timestamp ': timestamp, ' result ': result_list}
  24. return Json.dumps (Result)
  25. Class RealTimeData:
  26. def POST (self):
  27. data = Web.data ()
  28. Request_type = str (json.loads (data) [' type '])
  29. if Request_type = = ' getrealtimedata ':
  30. result = GetResult ()
  31. Web.header ("Access-control-allow-origin", "*")
  32. #web. Header ("Access-control-allow-methods", "POST, GET, OPTIONS, PUT, DELETE")
  33. #web. Header ("Access-control-allow-headers", "Accept, Content-type, content-length, \
  34. # accept-encoding, X-csrf-token, Authorization ")
  35. return result
  36. if __name__ = = "__main__":
  37. App.run ()


Only the "Access-control-allow-origin" limit is used, allowing requests from all sources. Start Httpserver:


    1. [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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.