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

2024-04-25 10:29:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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上使用此代码,它会崩溃(在第行=>;客户端.sendto(msg,address))出现以下错误:

^{pr2}$

你知道为什么和怎么修理吗?在


Tags: 代码import脚本clienthosttimeaddresswindows
1条回答
网友
1楼 · 发布于 2024-04-25 10:29:14

在这个讨论Sikuli如何与Jython进行互操作的线程中,您的问题得到了讨论并得到了明显的解决(这看起来像是一个Jython bug):https://bugs.launchpad.net/sikuli/+bug/1464105

我检查了解决方案;这段代码在sikulixide中适用于windows10。基本上在顶部添加了初始化头:

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

相关问题 更多 >