簡體   English   中英

在Django應用程序中查找主機和端口

[英]Find Host and Port in a Django Application

我正在連接到Twitter Streaming API,並正在設置OAuth握手。 我需要請求一個令牌,並發送一個callback_url作為params字典以及發布請求。

我已經將URL硬編碼用於開發(http://localhost:8000/oauth)但是在部署時,它會改變。 我想設置一些可以找到主機和端口的東西,並為其設置一個引用。 理想情況下,看起來像"http://%s/oauth" % (domain_name)

我試過同時使用os和socket模塊,代碼如下:

class OAuth:

    def request_oauthtoken(self):
        name = socket.gethostname()
        ip = socket.gethostbyname(name)
        domain = socket.gethostbyaddr(ip) # Sequence attempts to find the current domain name.  This will add expandability to the calling the API, as opposed to hard coding it in.  It's not returning what I'm expecting


        payload = { 'oauth_callback': 'http://localhost:8000/oauth' }
        print(domain)
        return payload

domain返回('justins-mbp-2.local.tld', ['145.15.168.192.in-addr.arpa'], ['192.168.15.145'])

name返回上面的元組的第一個元素, ip返回從集合中解開的元組的最后一個項目。

我正在尋找localhostlocalhost:8000的返回值。 我可以和任何一個一起工作。

調用request.build_absolute_uri() ,然后提取域。

文檔:

HttpRequest.build_absolute_uri(location)返回location的絕對URI形式。 如果未提供任何位置,則該位置將設置為request.get_full_path()。

如果該位置已經是絕對URI,則不會更改。 否則,將使用此請求中可用的服務器變量來構建絕對URI。

示例:“ http://example.com/music/bands/the_beatles/?print=true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM