import pyodbc import json import shutil import collections import time import os os.environ['TDSVER'] = '8.0' connstr = 'DRIVER={SQL Server};SERVER=SERVERNAME;DATABASE=DATABASENAME;' conn = pyodbc.connect(connstr) cursor = conn.cursor() cursor.execute(""" SELECT field1, field2, field3 FROM TABLENAME """) rows = cursor.fetchall() # Convert query to row arrays rowarray_list = [] for row in rows: t = (row.field1, row.field2, row.field3) rowarray_list.append(t) j = json.dumps(rowarray_list) rowarrays_file = 'C:\Production\calculation\marcWells_rowarrays.js' f = open(rowarrays_file,'w') print >> f, j # Convert query to objects of key-value pairs objects_list = [] for row in rows: d = collections.OrderedDict() d['field1'] = row.field1 d['field2'] = row.field2 d['field3'] = row.field3 objects_list.append(d) j = json.dumps(objects_list) objects_file = 'C:\Production\calculation\marcWells_objects.js' f = open(objects_file,'w') print >> f, j date_string = time.strftime("%Y%m%d%H%M%S") shutil.copyfile('C:\Production\input\marcWells_objects.js', r'C:\Production\input\marcWells_objects_' + date_string + '.js') conn.close()
mport shutil shutil.copyfile('C:\Production\calculation\marcWells_objects.js', r'C:\Production\input\marcWells_objects.js') conn.close()
def main(): import socket UDP_IP = '127.0.0.1' UDP_PORT = 5005 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind((UDP_IP,UDP_PORT)) while True: data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes print 'received message:', data if __name__ == '__main__': main()
def main(): import socket UDP_IP = '127.0.0.1' UDP_PORT = 5005 MESSAGE = 'Hi.' print 'UDP target IP:', UDP_IP print 'UDP target port:', UDP_PORT print 'Message:', MESSAGE sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(MESSAGE, (UDP_IP,UDP_PORT)) if __name__ == '__main__': main()
import socket UDP_IP = 'localhost' UDP_PORT = 5565 MESSAGE = 'Vehicle,124,Truck,25,2012-07-24T08:11:00,"-117.192914,34.056459"' print 'UDP target IP:', UDP_IP print 'UDP target port:', UDP_PORT print 'Message:', MESSAGE sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(MESSAGE, (UDP_IP,UDP_PORT))
import os, sys import time, argparse import socket def sendData(sock, dataPath, interval): dataFile = None i = 0 try: interval = float(interval / 1000.0) #open simulation data file and send data dataFile = open(dataPath, "r") print "Opened data file: %s" % dataPath for l in dataFile: sock.send(l) time.sleep(interval) dataFile.close() dataFile = None return True except Exception as e: raise Exception("Error sending data: %s" % e) finally: if dataFile: dataFile.close() #make argument parser to handle user command line input parser = argparse.ArgumentParser(description="Start a simulated data feed for a GeoEvent Processor tcp text input") parser.add_argument("-d", "--data", help="path to file containing csv data. Default is ../data/sources/AsdiUal_sub.csv", default="../data/sources/AsdiUal_sub.csv") parser.add_argument("-p", "--port", help="tcp socket port. Default is 5565", type=int, default=5565) parser.add_argument("-n", "--name", help="host name of server to connect to. Default is 'localhost'", default="localhost") parser.add_argument("-i", "--interval", help="interval in milliseconds between messages. Default is 500. Maximum interval is 5 seconds", type=int, default=500) if __name__ == "__main__": args = parser.parse_args() if args.interval > 5000: print "Warning: interval cannot be more than 5 seconds. Setting interval to 5 seconds" args.interval = 5000 if args.interval < 0: print "Warning: Invalid interval value. Setting interval to default value" args.interval = 1000 #begin simulation tcpSocket = None cont = True try: #Check if data file exists if not os.path.exists(args.data): raise Exception("Data file not found: %s." % args.data) #open socket tcpSocket = socket.create_connection((args.name, args.port)) print "Opened tcp socket on %(name)s:%(port)s" % {"name": args.name, "port": args.port} while cont: sendData(tcpSocket, args.data, args.interval) except Exception as e: print "Error: %s" % e finally: if tcpSocket: tcpSocket.close()
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.