Skip to content

Instantly share code, notes, and snippets.

@paulwinex
Last active February 4, 2018 12:46
Show Gist options
  • Save paulwinex/f8d903913f96e241e5a5099b99d195fe to your computer and use it in GitHub Desktop.
Save paulwinex/f8d903913f96e241e5a5099b99d195fe to your computer and use it in GitHub Desktop.
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