简体   繁体   English

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

[英].sendto() method on sikuli python script does not work on windows

I developed a sikuli python script on windows that uses this code: 我在使用以下代码的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(" "," ")

The code is working fine under linux or in a python script on windows, but if I use this code on sikulix on windows it crashes (at line => client.sendto(msg, address) ) with the following error: 该代码在linux或Windows上的python脚本中工作正常,但是如果我在Windows上的sikulix上使用此代码,则会崩溃(在line => client.sendto(msg,address) ),并显示以下错误:

[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 --------------

Any idea why and how to fix it? 知道为什么以及如何解决它吗?

Your problem was discussed and apparently solved in this thread discussing how Sikuli interops with Jython (and it seems like a Jython bug): https://bugs.launchpad.net/sikuli/+bug/1464105 在讨论Sikuli如何与Jython互操作(似乎是Jython错误)的线程中,您的问题得到了讨论并得到了解决: https ://bugs.launchpad.net/sikuli/+bug/1464105

I checked the solution; 我检查了解决方案; this code works for me on Windows 10 from within the SikuliX IDE. 此代码可从SikuliX IDE在Windows 10上为我使用。 The trick in basically in the added initialization header at the top: 技巧基本上在顶部添加的初始化标头中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM