web: can make POST request
This commit is contained in:
parent
0208a5d552
commit
88a8e0fe59
1 changed files with 13 additions and 8 deletions
|
@ -60,11 +60,12 @@ def getPassword(url):
|
||||||
|
|
||||||
# Get real pages
|
# Get real pages
|
||||||
|
|
||||||
def getURLContent(url, timeout=15):
|
def getURLContent(url, body=None, timeout=15):
|
||||||
"""Return page content corresponding to URL or None if any error occurs
|
"""Return page content corresponding to URL or None if any error occurs
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
url -- the URL to get
|
url -- the URL to get
|
||||||
|
body -- Data to send as POST content
|
||||||
timeout -- maximum number of seconds to wait before returning an exception
|
timeout -- maximum number of seconds to wait before returning an exception
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -89,11 +90,15 @@ def getURLContent(url, timeout=15):
|
||||||
try:
|
try:
|
||||||
from nemubot import __version__
|
from nemubot import __version__
|
||||||
if o.query != '':
|
if o.query != '':
|
||||||
conn.request("GET", o.path + "?" + o.query,
|
conn.request("GET" if body is None else "POST",
|
||||||
None, {"User-agent": "Nemubot v%s" % __version__})
|
o.path + "?" + o.query,
|
||||||
|
body,
|
||||||
|
{"User-agent": "Nemubot v%s" % __version__})
|
||||||
else:
|
else:
|
||||||
conn.request("GET", o.path, None, {"User-agent":
|
conn.request("GET" if body is None else "POST",
|
||||||
"Nemubot v%s" % __version__})
|
o.path,
|
||||||
|
body,
|
||||||
|
{"User-agent": "Nemubot v%s" % __version__})
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
raise IRCException(e.strerror)
|
raise IRCException(e.strerror)
|
||||||
|
|
||||||
|
@ -130,7 +135,7 @@ def getURLContent(url, timeout=15):
|
||||||
elif ((res.status == http.client.FOUND or
|
elif ((res.status == http.client.FOUND or
|
||||||
res.status == http.client.MOVED_PERMANENTLY) and
|
res.status == http.client.MOVED_PERMANENTLY) and
|
||||||
res.getheader("Location") != url):
|
res.getheader("Location") != url):
|
||||||
return getURLContent(res.getheader("Location"), timeout)
|
return getURLContent(res.getheader("Location"), timeout=timeout)
|
||||||
else:
|
else:
|
||||||
raise IRCException("A HTTP error occurs: %d - %s" %
|
raise IRCException("A HTTP error occurs: %d - %s" %
|
||||||
(res.status, http.client.responses[res.status]))
|
(res.status, http.client.responses[res.status]))
|
||||||
|
@ -144,7 +149,7 @@ def getXML(url, timeout=15):
|
||||||
timeout -- maximum number of seconds to wait before returning an exception
|
timeout -- maximum number of seconds to wait before returning an exception
|
||||||
"""
|
"""
|
||||||
|
|
||||||
cnt = getURLContent(url, timeout)
|
cnt = getURLContent(url, timeout=timeout)
|
||||||
if cnt is None:
|
if cnt is None:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
|
@ -162,7 +167,7 @@ def getJSON(url, timeout=15):
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
cnt = getURLContent(url, timeout)
|
cnt = getURLContent(url, timeout=timeout)
|
||||||
if cnt is None:
|
if cnt is None:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue