如何将httplib2部分转换为“扭曲”的方式?
from twisted.python import log, usage
from twisted.internet import reactor
from twisted.internet.serialport import SerialPort
from twisted.web.client import Agent
from struct import unpack
import httplib2
import sys
class JamesOptions(usage.Options):
optParameters = [
['outfile', 'o', None, 'Logfile [default: sys.stdout]'],
['baudrate', 'b', 38400, 'Serial baudrate [default: 9600'],
['port', 'p', '/dev/tty.usbserial-A600ezgH', 'Serial Port device'],
]
class XbeeLogger:
def decodeFloat(self, var):
text = ""
for i in range(0, len(var)):
text += var[i]
return unpack('f', text)[0]
def handle_packet(self, *args):
response = self._split_response(args)
h = httplib2.Http()
resp, content = h.request(
"http://localhost/emoncms2/api/post?apikey=2c0915af95786b73d841da00da68b5fa&json={real_power:" +
str(decodeFloat(response.get("rf_data")[0:4])) +
",apparent_power:" +
str(decodeFloat(response.get("rf_data")[4:8])) +
",power_factory:" +
str(decodeFloat(response.get("rf_data")[8:12])) +
",vrms:" +
str(decodeFloat(response.get("rf_data")[12:16])) +
",irms:" +
str(decodeFloat(response.get("rf_data")[16:20])) +
"}")
if __name__ == '__main__':
o = JamesOptions()
try:
o.parseOptions()
except usage.UsageError, errortext:
print '%s: %s' % (sys.argv[0], errortext)
print '%s: Try --help for usage details.' % (sys.argv[0])
raise SystemExit, 1
logFile = o.opts['outfile']
if logFile is None:
logFile = sys.stdout
log.startLogging(logFile)
from xbeeService.protocol import ZigBeeProtocol
class XbeeTest(ZigBeeProtocol, XbeeLogger):
pass
port = o.opts['port']
log.msg('Attempting to open %s at %dbps as a %s device' % (port, o.opts['baudrate'], ZigBeeProtocol.__name__))
s = SerialPort(XbeeTest(), o.opts['port'], reactor, baudrate=o.opts['baudrate'])
reactor.run()