【问题标题】:Python http get parametersPython http 获取参数
【发布时间】:2013-10-31 21:51:56
【问题描述】:

我构建了一个简单的服务器:

s = socket(AF_INET, SOCK_STREAM)
s.bind(('127.0.0.1',8282))
s.listen(1)
client, info = s.accept()

request = ""
i = 0

while True:
    c = client.recv(1)
    request += c
    if c in ["\r","\n"]:
        i += 1
    else:
        i = 0
    if i == 4:
        break

print "============="
print "Client Request"
print "============="
print request

raw_input('Press Enter to send response')

data = """<html>
<body>
<h1>My Amazing Website !!!</h1>
<br>
<font color='yellow' size='20'>Hello (Name)</font>
</body>
</html>
"""

response = "HTTP/1.1 200 OK\r\n"
response += "Content-Length: %d\r\n"%len(data)
response += "Connection: Close\r\n"
response += "Content-Type: text/html\r\n\r\n"
response += data

client.send(response)
time.sleep(2)
client.close()

现在我不明白如何用来自 url 的 get 替换我的 html 代码中的“(名称)”。假设我的网址是:127.0.0.1:8282/?name=Someone。 我怎样才能得到可以在这里找到的名字“GET /* HTTP/1.1?”

【问题讨论】:

    标签: python http parameters get


    【解决方案1】:

    Python 提供了 urlparse 模块,其中包含从 url 获取信息的实用程序!

    >>> from urlparse import urlparse, parse_qs
    >>> parse_qs(urlparse('/test?testing=test&greeting=smile').query)
    {'testing': ['test'], 'greeting': ['smile']}
    

    我认为这里的棘手部分是获取GET request path from the header

    python 提供了更高级别的方式来构建网络服务器。 https://wiki.python.org/moin/BaseHttpServer,以及一些优秀的全栈web框架

    【讨论】:

    • 谢谢,你能给我一个简单的方法来得到'get'吗?
    猜你喜欢
    • 2011-04-04
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 2013-11-27
    • 2013-02-07
    • 2019-08-05
    • 2023-03-09
    相关资源
    最近更新 更多