sikuli python脚本上的.sendto()方法在Windows上不起作用

时间:2017-11-12 18:56:03

标签: python sockets jython sikuli sendto

我在使用此代码的Windows上开发了一个sikuli python脚本:

from socket import AF_INET, SOCK_DGRAM
import sys
import socket
import struct, time

host = "pool.ntp.org"
port = 123
buf = 1024
address = (host,port)
msg = '\x1b' + 47 * '\0'

# reference time (in seconds since 1900-01-01 00:00:00)
TIME1970 = 2208988800L # 1970-01-01 00:00:00

# connect to server
client = socket.socket( AF_INET, SOCK_DGRAM)
client.sendto(msg, address)
msg, address = client.recvfrom( buf )

t = struct.unpack( "!12I", msg )[10]
t -= TIME1970

current_time = time.ctime(t).replace(" "," ")

代码在linux下或在Windows上的python脚本中工作正常,但如果我在Windows上的sikulix上使用此代码它会崩溃(在行=> client.sendto(msg,地址))出现以下错误:

[error] script [ Sikuli_Test_Original ] stopped with error in line 23
[error] _socket.error ( [Errno -1] Unmapped exception:     java.util.concurrent.RejectedExecutionException: event executor terminated )
[error] --- Traceback --- error source first line: module ( function ) statement 359: _socket ( handle_exception ) _socket.error: [Errno -1] Unmapped exception: java.util.concurrent.RejectedExecutionException: event executor terminated
995: _socket ( sendto ) File "C:\Users\myuser\Documents\Sikuli\sikulix.jar\Lib\_socket.py", line 971, in _datagram_connect
[error] --- Traceback --- end --------------

知道为什么以及如何解决它?

1 个答案:

答案 0 :(得分:0)

在讨论Sikuli如何与Jython进行交互(并且它看起来像是一个Jython错误)时,您的问题已经讨论并且显然得到了解决:https://bugs.launchpad.net/sikuli/+bug/1464105

我检查了解决方案;此代码适用于Windows 10上的SikuliX IDE。基本上在顶部添加的初始化头中的技巧:

import sys, _socket
from socket import AF_INET, SOCK_DGRAM
if _socket.NIO_GROUP.isShutdown():
    print "RE-CREATING NIO_GROUP"
    _socket.NIO_GROUP = _socket.NioEventLoopGroup(2, _socket.DaemonThreadFactory("PyScan-Netty-Client-%s"))
    sys.registerCloser(_socket._shutdown_threadpool)
import socket
import struct, time

host = "pool.ntp.org"
port = 123
buf = 1024
address = (host,port)
msg = '\x1b' + 47 * '\0'

# reference time (in seconds since 1900-01-01 00:00:00)
TIME1970 = 2208988800L # 1970-01-01 00:00:00
print "Before socket operation"
# connect to server
client = socket.socket( AF_INET, SOCK_DGRAM)
client.sendto(msg, address)
print "After socket operation"
msg, address = client.recvfrom( buf )
t = struct.unpack( "!12I", msg )[10]
t -= TIME1970

current_time = time.ctime(t).replace(" "," ")
print current_time