使用HTML作为Python程序的UI

Edit

问题

1. SimpleHTTPServer无法通过管道输出信息

在做第4步的时候,遇到了很大的挫折。第1步采用subprocess模块,打开命令python MyHTTPServer.py。对subprocess的介绍可参考:

遇到的问题是:

  • popen.communicate/read会block主进程,且block时,无法用ctrl+c退出
  • readline始终返回空字符

解决

最后解决的方案在这两个网页中:

简而言之,block是因为,没有字符输出到stdout,因为存在缓存,对python采用-u可以解决

2. Python Requests库在访问localhost时出503 Error

参看这个网页,来自Stack Overflow
Python requests 503 erros when trying to access localhost:8000
问题是因为公司代理禁止自己ping自己。

整理

SimpleHTTPServer基于BaseHTTPServer,并提供了文件列表下载服务。它是python为BaseHTTPServer提供的例程。开发者可以基于它开发,或者参考它,并基于BaseHTTPServer开发。

%23%u4F7F%u7528HTML%u4F5C%u4E3APython%u7A0B%u5E8F%u7684UI%0A@%28%u5DE5%u4F5C%u7B14%u8BB0%29%5Bpython%2C%20http%5D%0A%60%60%60sequence%0AUpdater-%3ESimpleHTTPServer%3A1.Start%0AUpdater-%3EHTML%3A%202.Open%20in%20Browser%0AHTML-%3ESimpleHTTPServer%3A%203.POST%0ASimpleHTTPServer-%3EUpdater%3A4.PIPE%0AUpdater-%3ESimpleHTTPServer%3A5.Terminate%0A%60%60%60%0A%0A%23%23%u95EE%u9898%0A%23%23%231.%20SimpleHTTPServer%u65E0%u6CD5%u901A%u8FC7%u7BA1%u9053%u8F93%u51FA%u4FE1%u606F%0A%u5728%u505A%u7B2C4%u6B65%u7684%u65F6%u5019%uFF0C%u9047%u5230%u4E86%u5F88%u5927%u7684%u632B%u6298%u3002%u7B2C1%u6B65%u91C7%u7528subprocess%u6A21%u5757%uFF0C%u6253%u5F00%u547D%u4EE4%60python%20MyHTTPServer.py%60%u3002%u5BF9subprocess%u7684%u4ECB%u7ECD%u53EF%u53C2%u8003%uFF1A%0A-%20%5B%20python%u5B50%u8FDB%u7A0B%u6A21%u5757subprocess%u8BE6%u89E3%u4E0E%u5E94%u7528%u5B9E%u4F8B%20%u4E4B%u4E00%5D%28https%3A//app.yinxiang.com/shard/s10/nl/161681/3fcc11e2-7d72-479f-9228-df72909bd465%29%0A-%20%5B%u5B98%u65B9%u6587%u6863%5D%28https%3A//docs.python.org/2/library/subprocess.html%23subprocess.CalledProcessError%29%0A%0A%u9047%u5230%u7684%u95EE%u9898%u662F%uFF1A%0A-%20popen.communicate/read%u4F1Ablock%u4E3B%u8FDB%u7A0B%uFF0C%u4E14block%u65F6%uFF0C%u65E0%u6CD5%u7528ctrl+c%u9000%u51FA%0A-%20readline%u59CB%u7EC8%u8FD4%u56DE%u7A7A%u5B57%u7B26%0A%0A%23%23%23%23%u89E3%u51B3%0A%u6700%u540E%u89E3%u51B3%u7684%u65B9%u6848%u5728%u8FD9%u4E24%u4E2A%u7F51%u9875%u4E2D%uFF1A%0A-%20%5BStartserver%20does%20not%20send%20data%20back%20to%20POpen.communicate%5D%28https%3A//app.yinxiang.com/shard/s10/nl/161681/81960cae-4da2-4526-9ec2-3888b3fee1fd%29%0A-%20%5BBypassing%20buffering%20of%20subprocess%20output%20with%20popen%20in%20C%20or%20Python%5D%28https%3A//app.yinxiang.com/shard/s10/nl/161681/ef454085-44d1-44c6-8862-42a889a855ab%29%0A%0A%u7B80%u800C%u8A00%u4E4B%uFF0Cblock%u662F%u56E0%u4E3A%uFF0C%u6CA1%u6709%u5B57%u7B26%u8F93%u51FA%u5230stdout%uFF0C%u56E0%u4E3A%u5B58%u5728%u7F13%u5B58%uFF0C%u5BF9python%u91C7%u7528%60-u%60%u53EF%u4EE5%u89E3%u51B3%0A%0A%23%23%232.%20Python%20Requests%u5E93%u5728%u8BBF%u95EElocalhost%u65F6%u51FA503%20Error%0A%u53C2%u770B%u8FD9%u4E2A%u7F51%u9875%uFF0C%u6765%u81EAStack%20Overflow%0A%5BPython%20requests%20503%20erros%20when%20trying%20to%20access%20localhost%3A8000%5D%28https%3A//app.yinxiang.com/shard/s10/nl/161681/272a1b96-68ed-4fc6-8bfa-4160c5f09907%29%0A%u95EE%u9898%u662F%u56E0%u4E3A%u516C%u53F8%u4EE3%u7406%u7981%u6B62%u81EA%u5DF1ping%u81EA%u5DF1%u3002%0A%0A%23%23%u6574%u7406%0ASimpleHTTPServer%u57FA%u4E8EBaseHTTPServer%uFF0C%u5E76%u63D0%u4F9B%u4E86%u6587%u4EF6%u5217%u8868%u4E0B%u8F7D%u670D%u52A1%u3002%u5B83%u662Fpython%u4E3ABaseHTTPServer%u63D0%u4F9B%u7684%u4F8B%u7A0B%u3002%u5F00%u53D1%u8005%u53EF%u4EE5%u57FA%u4E8E%u5B83%u5F00%u53D1%uFF0C%u6216%u8005%u53C2%u8003%u5B83%uFF0C%u5E76%u57FA%u4E8EBaseHTTPServer%u5F00%u53D1%u3002%0A%0A