I have a simple "hello world" python script here
print("Hello World")
I uploaded it to my site (similar to Pastebin or GitHub gist)
When I curl the link, I got
<title>
&<link>
tags come with it like this:<title>print("Hello World")</title><link rel="shortcut icon" href="https://i.imgur.com/yRk5mqb.png" /><pre>print("Hello World")</pre>
Which of course will break if I remotely executed my codes like this
curl https://www.bunlongheng.com/raw/YWFjOGMxYTktM2Y5Mi00ZDdlLWI1NWQtOTg0Y2EwYjVlMDM5 | python % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 153 0 153 0 0 354 0 --:--:-- --:--:-- --:--:-- 353 File "<stdin>", line 1 <title>print("Hello World")</title><link rel="shortcut icon" href="https://i.imgur.com/yRk5mqb.png" /><pre>print("Hello World")</pre> ^ SyntaxError: invalid syntax
How can I return only what's inside my
<pre>
tag if someone makes acURL
orwget
to my link?CodePudding user response:
You could attempt to sniff the
User-Agent
header but it isn't a good way to determine what type of content to provide to a client.A better approach (utilizing the features built into HTTP that are designed for handling different representations of the same content) might be to use the
Accept
header.
In Python, you could use the
accept-types
module for this.return_type = get_best_match(request.META.get('HTTP_ACCEPT'), ['text/html', 'application/python']) if return_type == 'application/python': return render_python() else: return render_html()
See this question for handling it in PHP.
Then the client could ask for the Python code if they wanted it:
curl -H "Accept: application/python" http://example.com/foo | python
CodePudding user response:
similar to Quentin, i would check if the Accept-header contains the string "html", and serve the html if it does. but if it does not explicitly mention html, serve the python directly. because all web-browsers say some version of
Accept: ...html...
(like Chrome saysAccept: text/html,application/xhtml xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
)while wget/curl/libcurl just says
Accept: */*