37 lines
882 B
Python
37 lines
882 B
Python
#!/usr/bin/env python3
|
|
"""
|
|
HTTP Example Action - Python Example Pack
|
|
|
|
Demonstrates using the `requests` library to make an HTTP call to example.com.
|
|
Receives parameters via stdin as JSON.
|
|
"""
|
|
|
|
import json
|
|
import sys
|
|
import urllib.request
|
|
|
|
|
|
def main():
|
|
# Read parameters from stdin (JSON format)
|
|
params = json.loads(sys.stdin.readline())
|
|
url = params.get("url", "https://example.com")
|
|
|
|
req = urllib.request.Request(url)
|
|
with urllib.request.urlopen(req, timeout=10) as response:
|
|
text = response.read().decode("utf-8")
|
|
status_code = response.status
|
|
final_url = response.url
|
|
|
|
result = {
|
|
"status_code": status_code,
|
|
"url": final_url,
|
|
"content_length": len(text),
|
|
"snippet": text[:500],
|
|
"success": 200 <= status_code < 400,
|
|
}
|
|
print(json.dumps(result))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|