我们做如下变更:
服务端把index.html变为response.json
{"name":"James","nation":"china","msg":"Hello World."}
增加一个自动的客户端请求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"
# 注意这是服务名
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()
运行请求客户端:
$ python request.py
运行图如下: