Skip to content

Commit 90c2879

Browse files
committed
add optional timeout parameter; fixes issue #16
1 parent 9f36416 commit 90c2879

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

ronkyuu/webmention.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def setParser(htmlParser='html5lib'):
3838
# processing stops)
3939

4040

41-
def findMentions(sourceURL, targetURL=None, exclude_domains=[], content=None, test_urls=True, headers={}):
41+
def findMentions(sourceURL, targetURL=None, exclude_domains=[], content=None, test_urls=True, headers={}, timeout=None):
4242
"""Find all <a /> elements in the given html for a post. Only scan html element matching all criteria in look_in.
4343
4444
optionally the content to be scanned can be given as an argument.
@@ -48,12 +48,14 @@ def findMentions(sourceURL, targetURL=None, exclude_domains=[], content=None, te
4848
4949
:param sourceURL: the URL for the post we are scanning
5050
:param exclude_domains: a list of domains to exclude from the search
51+
:type exclude_domains: list
5152
:param content: the content to be scanned for mentions
5253
:param look_in: dictionary with name, id and class_. only element matching all of these will be scanned
5354
:param test_urls: optional flag to test URLs for validation
5455
:param headers: optional headers to send with any web requests
55-
:type exclude_domains: list
5656
:type headers: dict
57+
:param timeout: optional timeout for web requests
58+
:type timeout float
5759
:rtype: dictionary of Mentions
5860
"""
5961

@@ -67,7 +69,7 @@ def findMentions(sourceURL, targetURL=None, exclude_domains=[], content=None, te
6769
'headers': None,
6870
}
6971
else:
70-
r = requests.get(sourceURL, verify=True, headers=headers)
72+
r = requests.get(sourceURL, verify=True, headers=headers, timeout=timeout)
7173
result = {'status': r.status_code,
7274
'headers': r.headers
7375
}
@@ -129,14 +131,17 @@ def findEndpoint(html):
129131
return None
130132

131133

132-
def discoverEndpoint(url, test_urls=True, debug=False, headers={}):
134+
def discoverEndpoint(url, test_urls=True, debug=False, headers={}, timeout=None):
133135
"""Discover any WebMention endpoint for a given URL.
134136
135137
:param link: URL to discover WebMention endpoint
136138
:param test_urls: optional flag to test URLs for validation
137139
:param debug: if true, then include in the returned tuple
138140
a list of debug entries
139141
:param headers: optional headers to send with any web requests
142+
:type headers dict
143+
:param timeout: optional timeout for web requests
144+
:type timeout float
140145
:rtype: tuple (status_code, URL, [debug])
141146
"""
142147
if test_urls:
@@ -146,7 +151,7 @@ def discoverEndpoint(url, test_urls=True, debug=False, headers={}):
146151
href = None
147152
d = []
148153
try:
149-
r = requests.get(url, verify=False, headers=headers)
154+
r = requests.get(url, verify=False, headers=headers, timeout=timeout)
150155
rc = r.status_code
151156
d.append('is url [%s] retrievable? [%s]' % (url, rc))
152157
if rc == requests.codes.ok:
@@ -173,7 +178,8 @@ def discoverEndpoint(url, test_urls=True, debug=False, headers={}):
173178
else:
174179
return (rc, href)
175180

176-
def sendWebmention(sourceURL, targetURL, webmention=None, test_urls=True, vouchDomain=None, debug=False, headers={}):
181+
def sendWebmention(sourceURL, targetURL, webmention=None, test_urls=True, vouchDomain=None,
182+
debug=False, headers={}, timeout=None):
177183
"""Send to the :targetURL: a WebMention for the :sourceURL:
178184
179185
The WebMention will be discovered if not given in the :webmention:
@@ -186,6 +192,10 @@ def sendWebmention(sourceURL, targetURL, webmention=None, test_urls=True, vouchD
186192
:param debug: if true, then include in the returned tuple
187193
a list of debug entries
188194
:param headers: optional headers to send with any web requests
195+
:type headers dict
196+
:param timeout: optional timeout for web requests
197+
:type timeout float
198+
189199
:rtype: HTTPrequest object if WebMention endpoint was valid
190200
"""
191201
if test_urls:
@@ -196,7 +206,7 @@ def sendWebmention(sourceURL, targetURL, webmention=None, test_urls=True, vouchD
196206
result = None
197207
d = []
198208
if webmention is None:
199-
wStatus, wUrl = discoverEndpoint(targetURL, debug=False, headers=headers)
209+
wStatus, wUrl = discoverEndpoint(targetURL, debug=False, headers=headers, timeout=timeout)
200210
else:
201211
wStatus = 200
202212
wUrl = webmention
@@ -212,15 +222,15 @@ def sendWebmention(sourceURL, targetURL, webmention=None, test_urls=True, vouchD
212222

213223
d.append('sending to [%s] %s' % (wUrl, payload))
214224
try:
215-
result = requests.post(wUrl, data=payload, headers=headers)
225+
result = requests.post(wUrl, data=payload, headers=headers, timeout=timeout)
216226
d.append('POST returned %d' % result.status_code)
217227

218228
if result.status_code == 405 and len(result.history) > 0:
219229
d.append('status code was 405, looking for redirect location')
220230
o = result.history[-1]
221231
if o.status_code == 301 and 'Location' in o.headers:
222232
d.append('redirected to [%s]' % o.headers['Location'])
223-
result = requests.post(o.headers['Location'], data=payload, headers=headers)
233+
result = requests.post(o.headers['Location'], data=payload, headers=headers, timeout=timeout)
224234
elif result.status_code not in (200, 201, 202):
225235
d.append('status code was not 200, 201, 202')
226236
except:

0 commit comments

Comments
 (0)