Last active
December 21, 2015 18:59
-
-
Save wiesson/6351251 to your computer and use it in GitHub Desktop.
MTConnect probe reader with requests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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