Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'subsequent': 0.05; 'parser': 0.07; 'skip:\\ 20': 0.07; 'socket': 0.07; 'sys': 0.07; 'caller': 0.09; 'ex:': 0.09; 'http': 0.09; 'msg': 0.09; 'resp': 0.09; 'skip:\\ 10': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'sfxlen:2': 0.11; 'python': 0.11; 'def': 0.12; '","': 0.16; '404': 0.16; 'hostname': 0.16; 'httperror': 0.16; 'subject:access': 0.16; 'true:': 0.16; 'url:windows': 0.16; "skip:' 30": 0.19; 'skip:p 40': 0.19; '8bit%:5': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'error': 0.23; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'skip:" 30': 0.26; 'skip:" 40': 0.26; 'skip:" 20': 0.27; 'skip:_ 20': 0.27; 'to:addr:python-dev': 0.27; 'host': 0.29; 'skip:p 30': 0.29; 'received:209.85.217': 0.29; 'errors': 0.30; 'skip:g 30': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'getting': 0.31; '"': 0.31; "skip:' 40": 0.31; 'class': 0.32; 'skip:m 30': 0.32; 'skip:& 30': 0.33; 'received:209.85': 0.35; 'except': 0.35; 'skip:s 30': 0.35; 'skip:u 20': 0.35; 'received:google.com': 0.35; '8bit%:9': 0.36; 'false': 0.36; 'ubuntu': 0.36; 'skip:4 10': 0.37; 'received:209': 0.37; 'skip:& 10': 0.38; 'skip:_ 30': 0.39; 'skip:& 20': 0.39; 'to:addr:python.org': 0.39; '8bit%:6': 0.40; 'skip:u 10': 0.60; 'skip:\xc2 10': 0.60; 'skip:a 30': 0.61; 'first': 0.61; '8bit%:10': 0.64; 'here': 0.66; 'skip:r 40': 0.68; 'skip:r 30': 0.69; 'azure': 0.84; 'httprequest,': 0.84; 'syed': 0.84; 'eliminates': 0.91 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:date:message-id:subject:from:to:cc :content-type; bh=w14yeysSiLg6T9B+lo+JF0ghA2+zVdaXqibcchsXMwM=; b=Ir4u2y29ffdJwOlqJxNcdNEk8WcxxuirfJa6W914nFs7oMO79bljXPWfN5acdjrONR i5jqd11OCsD3dJ532AZIOqgPQLEbVvuMaj9JbD2yitRLIWtL1fcaNB4+kC8WAU/wx6MS oLYgHSVKXe47pLckX3M8qWLd/5qqdRHzUKoiUeMftEQOFnfu0CoTOfSs2exYOYX5yD7q 2p+3lTalmh/ODjSPGCMjbDoylYy7aZOXZzYwW62asbxYBVNkhavmGtvo5/ZNN2dRzotY 3NVIOzFcBYFd5oVc4Kh9IajKkv8FOZMgCpm26IjocGbDehmJ7uUUz8uURFhGYZLdNq4B xzLg== X-Gm-Message-State: ALoCoQkepzZDmQICZHWhimbHj+bO6jpg8pBegCfz6NS18hgokmih24m2jBzEY1WfBIjuwV9dA/qp MIME-Version: 1.0 X-Received: by 10.152.5.167 with SMTP id t7mr1190999lat.32.1423200380354; Thu, 05 Feb 2015 21:26:20 -0800 (PST) X-Originating-IP: [98.155.101.136] Date: Thu, 5 Feb 2015 21:26:20 -0800 Subject: Azure event hub network access From: syed khalid To: python-dev@python.org Content-Type: multipart/alternative; boundary=089e01419d861d9d30050e64a9cd Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 297 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1423200697 news.xs4all.nl 2935 [2001:888:2000:d::a6]:60682 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:85279 --089e01419d861d9d30050e64a9cd Content-Type: text/plain; charset=UTF-8 I am getting http error 404. I am able to access the site via telnet which eliminates network issues. Here is the code and subsequent errors user/bin/python import sys import azure import socket from azure.servicebus import ( _service_bus_error_handler ) from azure.servicebus.servicebusservice import ( ServiceBusService, ServiceBusSASAuthentication ) from azure.http import ( HTTPRequest, HTTPError ) from azure.http.httpclient import _HTTPClient class EventHubClient(object): def sendMessage(self,body,partition): eventHubHost = "pac-ns.servicebus.windows.net" httpclient = _HTTPClient(service_instance=self) sasKeyName = "pac-pl" sasKeyValue = "IhkEepQPLfSy9jo6H2Yxxxxxxxxxxxxxxxxxxxx=" authentication = ServiceBusSASAuthentication(sasKeyName,sasKeyValue) request = HTTPRequest() request.method = "POST" request.host = eventHubHost request.protocol_override = "https" # request.path = "/myhub/publishers/" + partition + "/messages?api-version=20 14-05" request.body = body request.headers.append(('Content-Type', 'application/atom+xml;type=entry;cha rset=utf-8')) authentication.sign_request(request, httpclient) request.headers.append(('Content-Length', str(len(request.body)))) status = 0 try: resp = httpclient.perform_request(request) status = resp.status except HTTPError as ex: status = ex.status return status class EventDataParser(object): def getMessage(self,payload,sensorId): host = socket.gethostname() body = "{ \"DeviceId\" : \"" + host + "\",\"SensorData\": [ " msgs = payload.split(",") first = True for msg in msgs: # print msg sensorType = msg.split(":")[0] sensorValue = msg.split(":")[1] if first == True: first = False else: body += "," body += "{ \"SensorId\" : \"" + sensorId + "\", \"SensorType\" : \"" + sen sorType + "\", \"SensorValue\" : " + sensorValue + " }" body += "]}" return body hubClient = EventHubClient() parser = EventDataParser() hostname = socket.gethostname() sensor = sys.argv[2] body = parser.getMessage(sys.argv[1],sensor) hubStatus = hubClient.sendMessage(body,hostname) # return the HTTP status to the caller print hubStatus print hostname print sensor ~/IOT/AZURE$ python send.py temperature:22,humidity:20 deviceid 404 ubuntu deviceid { "DeviceId" : "ubuntu","SensorData": [ { "SensorId" : "deviceid", "SensorType" : "temperature", "SensorValue" : 22 },{ "SensorId" : "deviceid", "SensorType" : "humidity", "SensorValue" : 20 }]} -- *Syed Khalid* --089e01419d861d9d30050e64a9cd Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
I am gettin= g http error 404. I am able to access the site via telnet which eliminates = network issues. Here is the code and subsequent errors
<= div>
<= /font>


user/bin/python
import sys=
impor= t azure
import socket

from azure.servicebus im= port (
=C2=A0 _service_bus_error_handler
=C2=A0 )
<= br>
from azure.servicebus.servicebusservice import (<= /div>
=C2=A0= ServiceBusService,
=C2=A0 ServiceBusSASAuthentication
=C2= =A0 )

from azure.http import (
=C2=A0 HTTPRequ= est,
=C2=A0 HTTPError
=C2=A0 )

from az= ure.http.httpclient import _HTTPClient

class EventHubC= lient(object):

def sendMessage(self,body,partition):
=C2=A0 =C2=A0 eventHubHost =3D "pac-ns.servicebus.windows.net"

=C2=A0 =C2=A0 httpclient =3D _HTTPClient(service_instance=3Dself)

=C2=A0 =C2=A0 sasKeyName =3D "pac-pl"
=C2= =A0 =C2=A0 sasKeyValue =3D "IhkEepQPLfSy9jo6H2Yxxxxxxxxxxxxxxxxxxxx=3D= "

=C2=A0 =C2=A0 authentication =3D ServiceBusSASA= uthentication(sasKeyName,sasKeyValue)

= =C2=A0 =C2=A0 re= quest =3D HTTPRequest()
=C2=A0 =C2=A0 request.method =3D "POST= "
=C2=A0 =C2=A0 request.host =3D eventHubHost
=C2=A0 = =C2=A0 request.protocol_override =3D "https"
<= div># =C2=A0 =C2= =A0request.path =3D "/myhub/publishers/" + partition + "/mes= sages?api-version=3D20
14-05"
=C2=A0 =C2=A0 request.bo= dy =3D body
=C2=A0 =C2=A0 request.headers.append(('Content-Type= ', 'application/atom+xml;type=3Dentry;cha
<= font>rset=3Dutf-8'= ))

=C2=A0 =C2=A0 authentication.sign_request(request, = httpclient)

=C2=A0 =C2=A0 request.headers.append(('= ;Content-Length', str(len(request.body))))
status =3D 0<= /font>

=C2=A0 =C2=A0 try:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 resp = =3D httpclient.perform_request(request)
=C2=A0 =C2=A0 =C2=A0 =C2=A0= status =3D resp.status
=C2=A0 =C2=A0 except HTTPError as ex:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 status =3D ex.status

= =C2=A0 =C2=A0 return status

class EventDataParser(obje= ct):

=C2=A0 def getMessage(self,payload,sensorId):
=C2=A0 =C2=A0 host =3D socket.gethostname()
=C2=A0 =C2=A0 body= =3D "{ \"DeviceId\" : \"" + host + "\",= \"SensorData\": [ "

=C2=A0 =C2=A0 msgs = =3D payload.split(",")
=C2=A0 =C2=A0 first =3D True

=C2=A0 =C2=A0 for msg in msgs:
=C2=A0 =C2=A0 # pri= nt msg
=C2=A0 =C2=A0 =C2=A0 sensorType =3D msg.split(":")= [0]
sensorValue =3D msg.split(":")[1]
=
=C2=A0 =C2= =A0 =C2=A0 if first =3D=3D True:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 first= =3D False
=C2=A0 =C2=A0 =C2=A0 else:
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 body +=3D ","

<= span style=3D"background-color:rgba(255,255,255,0)">=C2=A0 =C2=A0 =C2=A0 bo= dy +=3D "{ \"SensorId\" : \"" + sensorId + "\= ", \"SensorType\" : \"" + sen
<= div>sorType + &q= uot;\", \"SensorValue\" : " + sensorValue + " }&qu= ot;
=C2=A0 =C2=A0 body +=3D "]}"
=
=C2= =A0 =C2=A0 return body

hubClient =3D EventHubClient()<= /span>
parser =3D EventDataParser()
hostname =3D socket.gethostnam= e()
sensor =3D sys.argv[2]

body =3D parser.get= Message(sys.argv[1],sensor)
hubStatus =3D hubClient.sendMessage(bod= y,hostname)
# return the HTTP status to the caller
print hu= bStatus
print hostname
print sensor



~/IOT/AZURE$ python send.py temperatur= e:22,humidity:20 deviceid=C2=A0

404
ubuntu
deviceid
{ "DeviceId" : "ubuntu","S= ensorData": [ { "SensorId" : "deviceid", "Sen= sorType" : "temperature", "SensorValue" : 22 },{ &= quot;SensorId" : "deviceid", "SensorType" : "= humidity", "SensorValue" : 20 }]}

=



--
Syed Khalid

--089e01419d861d9d30050e64a9cd--