torinaブログ

DjangoとBootstrap4で作成したブログ
Python, Django, Kivy, Bootstrap, Apache等のメモです
ソースコード

ほとんどのエンジニアには解けるパズル3

Python PythonHTTP
約400日前 2016年1月23日17:07
http://tango-ruby.hatenablog.com/entry/2016/01/10/125615
http://challenge-your-limits3.herokuapp.com/

またこのシリーズでございます
Hack Me, Baby
call/my/lovely/APIs
とあります、さっそくいきましょう
こんかいもPython3+requestsです
>>> import requests
>>> url = "http://challenge-your-limits3.herokuapp.com/call/my/lovely/APIs"
>>> res = requests.get(url)
>>> res.text
'{"message":"Let\'s get started! (1) Input whole numbers into \'/input/is/:x\' You can do it as much as you want.  (2) Guess w
hat is formula from the outcomes.  (3) Put your formula into \'formula/is/:answer\' e.g. \'formula/is/x+3\' Good luck!"}'



そろそろ改行コードも邪魔になってきたので、本来の方法と思われるjsonで見ましょう
>>> import json
>>> data = json.loads(res.text)
>>> data["message"]
"Let's get started! (1) Input whole numbers into '/input/is/:x' You can do it as much as you want.  (2) Guess what is formula
from the outcomes.  (3) Put your formula into 'formula/is/:answer' e.g. 'formula/is/x+3' Good luck!"



英語は正直中学生レベルでとまってるので、フィーリングでやっていきましょう
outputが19、手がかりはURLencodingみたいです
>>> url = "http://challenge-your-limits3.herokuapp.com/input/is/114514"
>>> res = requests.get(url)
>>> data = json.loads(res.text)
>>> data
{'output': 19, 'clue': 'URL encoding'}




ここからすげー時間がかかりました
http://challenge-your-limits3.herokuapp.com/input/is/114514 とかでやってたせいで変に混乱しましたが
0から順番に試すとわかりやすいでしょう
時間かかって悔しいので、答えは書かないです
>>> data
{'message': 'Good Job!! === Please register your lovely profile as /challengers'}


>>> d = {
...     "username":"torina",
...     "email":"toritoritorina@gmail.com",
... }
>>> res = requests.post("http://challenge-your-limits3.herokuapp.com/challengers", data=d)
>>> data = json.loads(res.text)
>>> data
{'username': 'torina', 'created_at': '2016-01-22T22:30:26.544Z', 'email': 'toritoritorina@gmail.com', 'message': "Thanks for y
our registration! Let's go to '/welcome/to/Iceland'", 'password': '9MBT3NZZmCom2W67', 'latitude': 35.69, 'longitude': 139.69,
'id': 4516, 'updated_at': '2016-01-22T22:30:26.544Z'}



不穏なメッセージが流れます
Icelandに引っ越さないといけないのでしょうか
>>> res = requests.get("http://challenge-your-limits3.herokuapp.com/welcome/to/Iceland", auth=("torina", "9MBT3NZZmCom2W67"))
>>> data = json.loads(res.text)
>>> data
{'error': "uh-oh. Sorry. This page can only be accessed within Iceland. You're accessing from Japan Tokyo. Only from Iceland i
s allowed. You wanna know why? Just ask 'why/Iceland'"}



why/Icelandを見てみましょう
/challengers/:idにPATCHでくるとアップデートできるよ、っていってますね
>>> res = requests.get("http://challenge-your-limits3.herokuapp.com/why/Iceland")
>>> data = json.loads(res.text)
>>> data
{'message': "Iceland is here === http://bit.ly/1OM90Vx  You know? There is NO NEED to use Iceland VPN. You can update your loc
ation from PATCH '/challengers/:id' Don't be so serious!"}



ちゃんと書き換わりましたね
>> d = {
..     'latitude': 48.87164,
..     'longitude':2.35275,
.. }
>> res = requests.patch("http://challenge-your-limits3.herokuapp.com/challengers/4516", auth=("torina", "9MBT3NZZmCom2W67"), data=d)
>> data = json.loads(res.text)
>> data
'username': 'torina', 'created_at': '2016-01-22T22:30:26.544Z', 'email': 'toritoritorina@gmail.com', 'updated_at': '2016-01-2
T22:45:41.146Z', 'password': '9MBT3NZZmCom2W67', 'id': 4516, 'longitude': 2.35275, 'latitude': 48.87164}



アクセスしましたが、書き換えた経度 緯度はフランスだったようです。間違えました
>>> res = requests.get("http://challenge-your-limits3.herokuapp.com/welcome/to/Iceland", auth=("torina", "9MBT3NZZmCom2W67"))
>>> data = json.loads(res.text)
>>> data
{'error': "uh-oh. Sorry. This page can only be accessed within Iceland. You're accessing from France IdF. Only from Iceland is
 allowed. You wanna know why? Just ask 'why/Iceland'"}



もう一度書き換え
>>> d = {
...     'latitude': 64.15000,
...     'longitude':-21.95000,
... }
>>> res = requests.patch("http://challenge-your-limits3.herokuapp.com/challengers/4516", auth=("torina", "9MBT3NZZmCom2W67"),
data=d)
>>> data = json.loads(res.text)
>>> data
{'username': 'torina', 'created_at': '2016-01-22T22:30:26.544Z', 'email': 'toritoritorina@gmail.com', 'updated_at': '2016-01-2
2T23:05:25.438Z', 'password': '9MBT3NZZmCom2W67', 'id': 4516, 'longitude': -21.95, 'latitude': 64.15}
>>> res = requests.get("http://challenge-your-limits3.herokuapp.com/welcome/to/Iceland", auth=("torina", "9MBT3NZZmCom2W67"))
>>> data = json.loads(res.text)
>>> data
{'message': 'Good Job! Welcome to Iceland!! === Please access to http://challenge-your-limits3.herokuapp.com/comments/token/RB
uHJdW1ObClMRsQZfQ from your web browser.'}



以上です、お疲れ様です。