test
This commit is contained in:
+4
-3
@@ -1,7 +1,8 @@
|
||||
from django.contrib import admin
|
||||
from django.urls import path, include
|
||||
from .views.GetIPDataView import GetIPData
|
||||
|
||||
from .views.GetIPDataView import GetIPDataView
|
||||
from .views.BaiduFanyiView import BaiduFanyiView
|
||||
urlpatterns = [
|
||||
path("getIpData/", GetIPData.as_view()),
|
||||
path("getIpData/", GetIPDataView.as_view()),
|
||||
path("baiduFanyi/", BaiduFanyiView.as_view()),
|
||||
]
|
||||
@@ -0,0 +1,48 @@
|
||||
from rest_framework.decorators import permission_classes
|
||||
from rest_framework.permissions import AllowAny
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
|
||||
import requests
|
||||
import json
|
||||
|
||||
import requests
|
||||
import random
|
||||
import json
|
||||
from hashlib import md5
|
||||
|
||||
|
||||
def make_md5(s, encoding='utf-8'):
|
||||
return md5(s.encode(encoding)).hexdigest()
|
||||
|
||||
@permission_classes([AllowAny])
|
||||
class BaiduFanyiView(APIView):
|
||||
def post(self, request):
|
||||
print(request.data)
|
||||
|
||||
# Set your own appid/appkey.
|
||||
appid = '20220718001275847'
|
||||
appkey = 'CJHmvf7qGs3szQy32cMg'
|
||||
|
||||
from_lang = 'en'
|
||||
to_lang = 'zh'
|
||||
|
||||
endpoint = 'http://api.fanyi.baidu.com'
|
||||
path = '/api/trans/vip/translate'
|
||||
url = endpoint + path
|
||||
|
||||
query = request.data['q']
|
||||
|
||||
salt = random.randint(32768, 65536)
|
||||
sign = make_md5(appid + query + str(salt) + appkey)
|
||||
|
||||
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
|
||||
payload = {'appid': appid, 'q': query, 'from': from_lang, 'to': to_lang, 'salt': salt, 'sign': sign}
|
||||
|
||||
r = requests.post(url, params=payload, headers=headers)
|
||||
result = r.json()
|
||||
|
||||
|
||||
return Response(result, status=status.HTTP_200_OK)
|
||||
@@ -6,7 +6,7 @@ from rest_framework.permissions import AllowAny
|
||||
|
||||
|
||||
@permission_classes([AllowAny])
|
||||
class GetIPData(APIView):
|
||||
class GetIPDataView(APIView):
|
||||
def get(self, request):
|
||||
ip_info = {
|
||||
'remote_addr': request.META.get('REMOTE_ADDR'),
|
||||
@@ -19,4 +19,6 @@ class GetIPData(APIView):
|
||||
'http_forwarded': request.META.get('HTTP_FORWARDED'),
|
||||
}
|
||||
|
||||
return Response({"ip_info": ip_info}, status=status.HTTP_200_OK)
|
||||
return Response({"ip_info": ip_info}, status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user