Skip to content

Instantly share code, notes, and snippets.

@wiesson
Last active December 21, 2015 18:59
Show Gist options
  • Save wiesson/6351251 to your computer and use it in GitHub Desktop.
Save wiesson/6351251 to your computer and use it in GitHub Desktop.
MTConnect probe reader with requests
# import requests
namespace = True
def mt_request():
url = 'http://agent.mtconnect.org/sample'
payload = { 'interval': '1000', 'count':'20' }
r = requests.get(url, params=payload, stream=True)
part = []
for line in r.iter_lines():
if line.startswith('--'): # start of new chunk
if not part:
continue # Handle empty parts
strpart = '\r\n'.join(part[2:])
headers = ''.join(part[0:2])
body = ''.join(strpart.split('\r\n\r\n',1))
print (headers, body)
# Clean up for the next chunk.
part = []
elif line: # Portion of current chunk.
if namespace:
if line.startswith('<MTConnectStreams'):
line = '<MTConnectStreams>'
if line.startswith('<MTConnectAssets'):
line = ' <MTConnectAssets> '
if line.startswith('<MTConnectDevices'):
line = ' <MTConnectDevices> '
part.append(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment