Pythonソケット


remoteならばライセンス違反か?
同一筐体/同一ユーザのArcPy読み込み等の初期化処理速度向上ならば許されねぇかな。

クライアント側

# -*- coding: cp932 -*-

import socket
import sys

HOST, PORT = "localhost", 9999
data = "test"

# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect to server and send data
sock.connect((HOST, PORT))
sock.send(data + "\n")

# Receive data from the server and shut down
received = sock.recv(1024)
sock.close()

print "Sent:     %s" % data
print "Received: %s" % received

サーバ側

# -*- coding: cp932 -*-
import SocketServer

import arcpy
import numpy
import math
import time
import datetime


class TestServer(SocketServer.TCPServer):
     def SetSde (self):
         self.sde = arcpy.ArcSDESQLExecute(r'c:\test.sde')
         
class MyTCPHandler(SocketServer.BaseRequestHandler):
    
    def handle(self):
        
        self.data = self.request.recv(1024).strip()
        print "%s wrote:" % self.client_address[0]
        print self.data

        d = datetime.datetime.today()
        outdata = r"C:\tmp\tmp.gdb" + "\\R" +  d.strftime("%Y_%m_%d_%H_%M_%S")
        
        orgX = 120
        orgY = 20

        width = 1
        height = 1

        colX = 30
        rowY = 30

        start = time.time()

        sql_execute = self.server.sde
        xyarray = sql_execute.execute("SELECT Shape.STX X, Shape.STY Y FROM RND10K")

        # 数量分0埋め配列作って reshapeでXYにする。
        npArray = numpy.zeros(colX * rowY, numpy.int32).reshape(rowY, colX)

        for xy in xyarray:
            w = math.floor(xy[0] - orgX / width)
            h = math.floor(xy[1] - orgY / height)
            npArray[w][h] += 1
                
        print "end loop:", time.time() - start

        raster = arcpy.NumPyArrayToRaster(npArray,
                                          arcpy.Point(orgX, orgY),
                                          width, height)

        raster.save(outdata)

        print "raster:", time.time() - start
        
        self.request.send(arcpy.GetMessages())

if __name__ == "__main__":
    HOST, PORT = "localhost", 9999

    # Create the server, binding to localhost on port 9999
    server = TestServer((HOST, PORT), MyTCPHandler)
    server.SetSde()
    print "RUN"
    
    # Activate the server; this will keep running until you
    # interrupt the program with Ctrl-C
    server.serve_forever()
カテゴリー: 開発 タグ: パーマリンク