Last active
February 4, 2018 12:46
-
-
Save paulwinex/f8d903913f96e241e5a5099b99d195fe to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import socket, json | |
from httplib import HTTPResponse | |
def get_port(): | |
# some function | |
return 57980 | |
def request(path, **kwargs): | |
HOST = 'localhost' | |
PORT = get_port() | |
DATA_POST = json.dumps(kwargs) | |
DATA_LEN = str(len(DATA_POST)) | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect((HOST, PORT)) | |
s.send("POST /{path} HTTP/1.1\r\nHost: {host}\r\nContent-Type: application/json\r\nContent-Length: {len}\r\n\r\n{data}".format( | |
path=path, | |
host=HOST, | |
len=DATA_LEN, | |
data=DATA_POST | |
)) | |
r = HTTPResponse(s) | |
r.begin() | |
s.close() | |
data = json.loads(r.read()) | |
return data | |
request('test', key='value') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment