Writing response body with BaseHTTPRequestHandler

前端 未结 4 1448
挽巷
挽巷 2021-02-03 21:06

I\'m playing a little with Python 3.2.2 and want to write a simple web server to access some data remotely. This data will be generated by Python so I don\'t want to use the Sim

相关标签:
4条回答
  • 2021-02-03 21:40

    For Python 3, prefix the string literals with a b:

    self.wfile.write(b"<foo>bar</foo>")
    
    0 讨论(0)
  • 2021-02-03 21:42

    Just use this when using Python 3.X

    self.wfile.write(bytes("<body><p>This is a test.</p>", "utf-8"))
    
    0 讨论(0)
  • 2021-02-03 21:47

    based on your code #comments you're probably looking for self.headers.getheaders('referer'), ie:

    if 'http://www.icamefromthissite.com/' in self.headers.getheaders('referer'):
        do something
    
    0 讨论(0)
  • 2021-02-03 21:52

    In Python3 string is a different type than that in Python 2.x. Cast it into bytes using either

    self.wfile.write(bytes("<html><head><title>Title goes here.</title></head>/html>","utf-8")) 
    

    or

    self.wfile.write("<html><head><title>Title goes here.</title></head></html>".encode("utf-8"))
    
    0 讨论(0)
提交回复
热议问题