Last active
October 14, 2015 18:20
-
-
Save indraastra/6ba088ef4f7d5b1fb5af to your computer and use it in GitHub Desktop.
one or two packets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# REQUIRES PYTHON 3 | |
# | |
# $ python3 packet_test.py 1 | |
# sending 1 packet | |
# {'r': -1024} | |
# $ python3 packet_test.py 2 | |
# sending 2 packets | |
# {'r': 0} | |
import socket | |
import json | |
import sys | |
from http.client import HTTPResponse | |
from io import BytesIO | |
# Modified from http://stackoverflow.com/a/24729316 | |
class FakeSocket(): | |
def __init__(self, response_str): | |
self._file = BytesIO(response_str) | |
def makefile(self, *args, **kwargs): | |
return self._file | |
BUFFER_SIZE = 1024 | |
TCP_IP = '192.168.0.1' | |
TCP_PORT=80 | |
HEADERS = b'POST /configure-ap HTTP/1.1\r\nAccept: */*\r\nAccept-Encoding: gzip, deflate\r\nContent-Length: 53\r\nContent-Type: application/x-www-form-urlencoded\r\nAccept-Language: en-us\r\nConnection: keep-alive\r\nUser-Agent: PhotonWifiConfig/1 CFNetwork/758.0.2 Darwin/14.5.0\r\nPragma: no-cache\r\nCache-Control: no-cache\r\nHost: 192.168.0.1\r\n\r\n' | |
# NOTE: This body should elicit a negative response from the server (-1024) | |
# since the `pwd` field is not a valid RSA-encoded key. | |
BODY = b'{"ssid":"blah","pwd":"","ch":1,"sec":4194308,"idx":0}' | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect((TCP_IP, TCP_PORT)) | |
if sys.argv[1] == "1": | |
# WORKS! | |
print("sending 1 packet") | |
s.send(HEADERS + BODY); | |
else: | |
# FAILS! | |
print("sending 2 packets") | |
s.send(HEADERS) | |
s.send(BODY) | |
raw_response = s.recv(BUFFER_SIZE) | |
parsed_response = HTTPResponse(FakeSocket(raw_response)) | |
parsed_response.begin() | |
s.close() | |
print(json.loads(parsed_response.read().decode("ascii"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment