Skip to content

Instantly share code, notes, and snippets.

@qrtt1
Created May 26, 2020 00:32
Show Gist options
  • Save qrtt1/94212b6606de20780a626db973a0b4b8 to your computer and use it in GitHub Desktop.
Save qrtt1/94212b6606de20780a626db973a0b4b8 to your computer and use it in GitHub Desktop.
import collections
import re
import urllib
from urllib.request import Request
_s1 = r"""
curl 'https://irs.thsrc.com.tw/IMINT/captImg/foobar.jpg' \
-H 'Connection: keep-alive' \
-H 'Pragma: no-cache' \
-H 'Cache-Control: no-cache' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36' \
-H 'Accept: image/webp,image/apng,image/*,*/*;q=0.8' \
-H 'Sec-Fetch-Site: same-origin' \
-H 'Sec-Fetch-Mode: no-cors' \
-H 'Sec-Fetch-Dest: image' \
-H 'Referer: https://irs.thsrc.com.tw/IMINT/?locale=tw' \
-H 'Accept-Language: zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7' \
-H $'Cookie: name=value; JSESSIONID=33638C21C70E546422E8B5B8624A4032; _ga=GA1.3.1021411901.1590451264; _gid=GA1.3.293025812.1590451164; vpadn-ctid=aebbf92b-51c8-2b56-057f-3ad3741f5cc8; vpadn-seid=vp86947895991-15904511644; vpadn-vpid=aebbf92b-51c8-2b56-057f-3ad3741f5cc8; vpadn-ce=1; vpadn-sd=1590451165760; IRS-SESSION=\u0021PD2p5+EpWpyr8i9ueT2fDEgNU4eNgURu4bi3zesFPckICGGzWaeG14mPLVfo++Ii; THSRC-IRS=\u0021ihLTC+Rn6o5HmBAtrhu6K7HqeZtYwFQp9nyBm0V71wCBu7yE/e/zV2CQ9On1eQ7owxLhYePVpjx32A==; bm_mi=B46BAD9348B1450E1B428BEC627A4285~NkPHbiUNfUJrHlGpPj2f+WBPXvzEvyP2E+fZ05eWbKRDSv0Lg6r1hRV76ABqw3almwpIwScTHvqVacQRLsJoXpQE37x2XNDGdHrjT4IFe9+oWi9E0/k2ACXvD49pFaiGzYjohqY3xEOtC0rtpkG/n1KxlFYLrT/HGolVdDdwTxoO6bjKF+ElGIKt3bm2WplolWKQrPtvruZsRlmTfjBbOj7rM0WWIkPeQI5S/UvVTbDwo1B+/PDwjjsfwxWQD2xyJ/m7a0kYjxjZ7Rb4YUrrHQ==; _fbp=fb.2.1590451176499.797356158; ak_bmsc=D402D8AD34646D17F88E51AC8474869A17D2D76E7E470000E75BCC5E9586EE72~plL8l3/Junb4W1Xis6xNgZ8//7t02tgr07h970OZfdBFfvGkdwoI1AjnOcQcbhrAfSqUIANsjP9ikDJeL74udKOsXELbylncPRj0Mpv7lunZPbgD5I8z3xrv/EZcG1mr+RerkJyjLuVZh4WBGUjAtF3J6MwqTS9Ko2Fkr5mYumVRiW0Ws3JQ7NzXhcwPU2YvSaFD4ZRSmig7HExCOtZSsIYYcc6oxQBuju28E322sRwz4xtUymD3ERQRRqXEkQVHO4; AcceptIRSCookiePolicyTime=Tue%20May%2026%202020%2007:59:36%20GMT+0800%20(%E5%8F%B0%E5%8C%97%E6%A8%99%E6%BA%96%E6%99%82%E9%96%93); bm_sv=425DC257BF018E156AE54DBDC26DCE88~re1uCPF6BOaavF7xJrFyl8kK4Zxv1z2PPd7eAy/lwXFu+yu9IFWHdwyFluLUyj1YYpgsP8YsRnuyv4v38U9gyRej1x3f9dcnPVcCG1ALKe1Lf4R0AlgdGUEsHXlVYdcZI8JfNhYQ+hqQZFisf68kD2afXc59d4zcKm63VppEDL0=' \
--compressed
"""
def build_headers_from_curl(curl_command: str) -> dict:
headers = collections.OrderedDict()
for m in re.findall(r"-H \$?'([^']+)'", curl_command):
header = re.findall(r"(^[^:]+): ?(.+)$", m)
if header:
header = header[0]
headers[header[0]] = header[1]
return headers
if __name__ == '__main__':
headers = build_headers_from_curl(_s1)
request = Request(url="https://irs.thsrc.com.tw/IMINT/captImg/foobar.jpg", headers=headers)
response = urllib.request.urlopen(request)
with open("out.jpg", "wb") as fh:
fh.write(response.read())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment