django API 中接口的互相调用

``

	url = "http://%s:%s/api-token-auth/" % (ip, port)
    query_args = {
        "username": username,
        "password": password
    }
    resp = requests.post(url=url, data=query_args)
    token = json.loads(resp.text)["token"]
    headers = {"Authorization": "JWT" + " " + token}     #  拿到token,拼成headers


	post_url = "http://%s:%s/message/message-level-two/"% (ip, port)
    data = {
        "app": app,
        "url": url,
        "message_id": message_id,
        "head": head,
        "title": title,
        "userprofile_id_list": userprofile_id_list
    }
    headers = self.headers
    requests.post(url=post_url, data=data, headers=headers)

获取当前请求的ip和端口

		host_ip, host_port = self.request.META.get("HTTP_HOST").split(':')[0], \
                             self.request.META.get("HTTP_HOST").split(':')[1]

常见的请求头如下:

CONTENT_LENGTH – The length of the request body (as a string).
CONTENT_TYPE – The MIME type of the request body.
HTTP_ACCEPT – Acceptable content types for the response.
HTTP_ACCEPT_ENCODING – Acceptable encodings for the response.
HTTP_ACCEPT_LANGUAGE – Acceptable languages for the response.
HTTP_HOST – The HTTP Host header sent by the client.
HTTP_REFERER – The referring page, if any.
HTTP_USER_AGENT – The client’s user-agent string.
QUERY_STRING – The query string, as a single (unparsed) string.
REMOTE_ADDR – The IP address of the client.
REMOTE_HOST – The hostname of the client.
REMOTE_USER – The user authenticated by the Web server, if any.
REQUEST_METHOD – A string such as "GET" or "POST".
SERVER_NAME – The hostname of the server.
SERVER_PORT – The port of the server (as a string).   

获取请求头内容的用META
示例:

def index(request):
    ip = request.META.get("REMOTE_ADDR")
    return HttpResponse("你的ip地址是%s"%ip)

http://10.254.30.27/1
self.kwargs[‘pk’] # 可以拿到后边的 1

你可能感兴趣的:(django API 中接口的互相调用)