我們做如下變更:
服務(wù)端把index.html變?yōu)閞esponse.json
{"name":"James","nation":"china","msg":"Hello World."}
增加一個(gè)自動(dòng)的客戶端請(qǐng)求request.py
import urllib
import urllib.request as urllib2
import requests
import threading,time
class SendRequest(threading.Thread):
def __init__(self,interval,count):
threading.Thread.__init__(self)
self.interval = interval
self.count = count
def run(self):
try:
url = "http://localhost:4140/response.json"
# 注意這是服務(wù)名
headers={"Host":"web"}
result = requests.get(url,headers=headers)
print(time.strftime('%Y-%m-%d %X',time.localtime())+" - "+str(self.count)+" - "+result.text)
self.count = self.count+1
#time.sleep(1)
except Exception as e:
print(e)
finally:
SendRequest(self.interval,self.count).start()
if __name__ == '__main__':
count = 1
SendRequest(0.1,count).start()
運(yùn)行請(qǐng)求客戶端:
$ python request.py
運(yùn)行圖如下:

火狐截圖_2017-12-26T07-53-45.248Z.png