This commit is contained in:
Bigsk 2020-12-27 14:17:58 +08:00
parent 57310a38f2
commit cb036b7cb8
2 changed files with 78 additions and 15 deletions

View File

@ -27,7 +27,6 @@ class APICore(object):
self.Log(addr[0],addr[1],"SEND",Result)
sock.send(str(Result).encode("utf-8"))
def SunAzEl(self,sock,addr):
sock.send(self.HTTPHead['200'].encode("utf-8"))
Result=json.dumps(Astronomy().calcSunAzEl())
self.Log(addr[0],addr[1],"SEND",Result)
sock.send(str(Result).encode("utf-8"))
@ -42,16 +41,26 @@ def socket(sock,addr):
while True:
try:
data = sock.recv(1024)
API.Log(addr[0],addr[1],"RECV","Received a request.")
#print(data.decode("utf-8"))
if("HTTP/" in data.decode("utf-8")):
API.Log(addr[0],addr[1],"RECV","Received a HTTP request.")
#HTTP Request.
HTTPRequestHead=data.decode("utf-8").split("\r\n")[0].split(" ")[1].split("/")
del HTTPRequestHead[0]
for i in range(0,len(HTTPRequestHead)):
HTTPRequestHead[i]=urllib.parse.unquote(HTTPRequestHead[i])
try:
Heada=data.decode("utf-8").split("\r\n")[0].split(" ")[1].split("/")
del Heada[0]
except:
API.Error400(sock,addr)
break
try:
Headb=data.decode("utf-8").split("\r\n\r\n")[1]
except:
Headb=""
HTTPRequestHead=(Heada,Headb)
for i in range(0,len(HTTPRequestHead[0])):
HTTPRequestHead[0][i]=urllib.parse.unquote(HTTPRequestHead[0][i])
API.Log(addr[0],addr[1],"RECV",HTTPRequestHead)
if(" / " in data.decode("utf-8")):
#Help Command
sock.send(API.HTTPHead['200'].encode("utf-8"))
Result=json.dumps({"wikis":"https://gitee.com/ghink/api/wikis"})
API.Log(addr[0],addr[1],"SEND",Result)
@ -59,38 +68,92 @@ def socket(sock,addr):
break
elif(" /SunAzEl" in data.decode("utf-8")):
if("GET " in data.decode("utf-8")):
#GET Request
try:
HTTPRequestHead[1]
if(API.IsJson(HTTPRequestHead[1])):
API.SunAzEl(sock,addr)
#Exist Parameter
HTTPRequestHead[0][1]
if(API.IsJson(HTTPRequestHead[0][1])):
#Is Json,Return Result
try:
Para=json.loads(HTTPRequestHead[0][1])
Result=json.dumps(Astronomy().calcSunAzEl(Para['Stamp'],Para['Lon'],Para['Lat'],Para['TimeZone'],Para['ZeroAzimuth']))
sock.send(API.HTTPHead['200'].encode("utf-8"))
API.Log(addr[0],addr[1],"SEND",Result)
sock.send(str(Result).encode("utf-8"))
except:
API.Error400(sock,addr)
else:
#Not Json,Return Bad Request
API.Error400(sock,addr)
except:
#No Parameter
sock.send(API.HTTPHead['200'].encode("utf-8"))
API.SunAzEl(sock,addr)
elif("POST " in data.decode("utf-8")):
#POST Request
try:
HTTPRequestHead[1]
#Exist GET Parameter,Return Bad Request
HTTPRequestHead[0][1]
API.Error400(sock,addr)
except:
API.SunAzEl(sock,addr)
#No GET Parameter,Continue
if(API.IsJson(HTTPRequestHead[1])):
#If POST Parameter is Json
try:
Para=json.loads(HTTPRequestHead[1])
Result=json.dumps(Astronomy().calcSunAzEl(Para['Stamp'],Para['Lon'],Para['Lat'],Para['TimeZone'],Para['ZeroAzimuth']))
sock.send(API.HTTPHead['200'].encode("utf-8"))
API.Log(addr[0],addr[1],"SEND",Result)
sock.send(str(Result).encode("utf-8"))
except:
API.Error400(sock,addr)
elif(HTTPRequestHead[1]==""):
#If No POST Parameter
sock.send(API.HTTPHead['200'].encode("utf-8"))
API.SunAzEl(sock,addr)
else:
#Wrong POST Parameter,Return Bad Request
API.Error400(sock,addr)
break
else:
#No Such Command
sock.send(API.HTTPHead['404'].encode("utf8"))
Result=json.dumps({"info":"not found","code":404})
API.Log(addr[0],addr[1],"SEND",Result)
sock.send(Result.encode("utf-8"))
break
else:
API.Log(addr[0],addr[1],"RECV","Received a TCP request.")
#TCP Request.
if(data.decode("utf-8")=="Help" or data.decode("utf-8")=="HELP" or data.decode("utf-8")=="help" or data.decode("utf-8")=="WIKI" or data.decode("utf-8")=="wiki" or data.decode("utf-8")=="wikis" or data.decode("utf-8")=="WIKIS" or data.decode("utf-8")=="?"):
#Help Command
Result=json.dumps({"wikis":"https://gitee.com/ghink/api/wikis"})
API.Log(addr[0],addr[1],"SEND",Result)
sock.send(str(Result).encode("utf8"))
elif(data.decode("utf-8")=="SunAzEl"):
Result=json.dumps(Astronomy().calcSunAzEl())
API.Log(addr[0],addr[1],"SEND",Result)
sock.send(str(Result).encode("utf-8"))
elif("SunAzEl" in data.decode("utf-8")):
if(API.IsJson(data.decode("utf-8"))):
#Is Json
try:
#Check whether is right json
Para=json.loads(data.decode("utf-8"))['Parameter']
Result=json.dumps(Astronomy().calcSunAzEl(Para['Stamp'],Para['Lon'],Para['Lat'],Para['TimeZone'],Para['ZeroAzimuth']))
API.Log(addr[0],addr[1],"SEND",Result)
sock.send(str(Result).encode("utf-8"))
except:
#Not right json
Result=json.dumps({"info":"bad request","code":400})
API.Log(addr[0],addr[1],"SEND",Result)
sock.send(Result.encode("utf-8"))
elif(data.decode("utf-8")=="SunAzEl"):
#Not Json
API.SunAzEl(sock,addr)
else:
#Wrong Command
Result=json.dumps({"info":"bad request","code":400})
API.Log(addr[0],addr[1],"SEND",Result)
sock.send(Result.encode("utf-8"))
else:
#No Such Command
Result=json.dumps({"info":"not found","code":404})
API.Log(addr[0],addr[1],"SEND",Result)
sock.send(Result.encode("utf-8"))