Skip to content

Instantly share code, notes, and snippets.

@yheihei
Created August 9, 2016 14:30
Show Gist options
  • Save yheihei/6bdd9a8c3fec5ee8e66d5a0800d5504e to your computer and use it in GitHub Desktop.
Save yheihei/6bdd9a8c3fec5ee8e66d5a0800d5504e to your computer and use it in GitHub Desktop.
# -*- coding:utf-8 -*-
import httplib
import json
import urllib
import traceback
svr = '127.0.0.1:8000'
h = None
data = None
def connect():
global h
if h != None: return
h = httplib.HTTPConnection(svr)
def close():
global h
if h == None: return
h.close()
h = None
def get():
connect()
try:
h.request('GET', '/vehicle/12345678')
except:
traceback.print_exc()
close()
return
response = h.getresponse()
if response.status == httplib.OK:
body = response.read()
global data
data = json.loads(body)
print data
else:
print response.status
close()
def put():
global data
if data == None: return
connect()
data['Components']['GW'] = data['Components']['GW'] + 1
data['Components']['ECU1'] = data['Components']['ECU1'] + 1
try:
h.request('PUT', '/vehicle/12345678', json.dumps(data))
except:
traceback.print_exc()
close()
data['Components']['GW'] = data['Components']['GW'] - 1
data['Components']['ECU1'] = data['Components']['ECU1'] - 1
return
close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment