【问题标题】:.sendto() method on sikuli python script does not work on windowssikuli python脚本上的.sendto()方法在windows上不起作用
【发布时间】:2018-04-25 10:41:17
【问题描述】:

我在 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 上使用此代码,它会崩溃(在 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 --------------

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

【问题讨论】:

  • 从错误日志中我的印象是 sikuli 试图使用自己的 _socket.py 而不是系统的
  • 您是否在两个系统上使用相同的 sikuli 版本?
  • 是的,最新版的sikulix
  • 那么问题出在 Jython 本身。因为 sikuli 在后台使用 Jython。然后唯一的解决方法是在 sikuli 中使用 run() 函数并将 python 文件作为外部脚本运行。
  • 您能否确认系统要求已满github.com/kevlened/sikuli_cpython#requirements

标签: python sockets jython sikuli sendto


【解决方案1】:

您的问题已在讨论 Sikuli 如何与 Jython 互操作的主题中得到讨论并明显解决(这似乎是一个 Jython 错误):https://bugs.launchpad.net/sikuli/+bug/1464105

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 2021-07-28
    • 2014-01-17
    • 1970-01-01
    • 2018-10-20
    • 1970-01-01
    相关资源
    最近更新 更多