通过'jython -m CGIHTTPServer‘使用jython作为CGI服务器会导致C-Python中没有的错误:error: (20000, 'socket must be in non-blocking mode').
如果有人(像我一样)想要使用jython作为jython模型、脚本等的简单CGI-Server,这是不可接受的。我找到了一个解决方案,希望这也能对其他人有所帮助:
编辑文件jython/Lib/select.py并转到标记的行,然后添加带有箭头的两行(见下文)。然后,正如C-Python中已知的那样,一切都工作得很好。
jython/Lib/select.py:
...
class poll:
...
def register(self, socket_object, mask = POLLIN|POLLOUT|POLLPRI):
try:
try: socket_object.setblocking(0) # <-- line to add
except: pass # <-- line to add
channel = _getselectable(socket_object)
if channel is None:
# The socket is not yet connected, and thus has no channel
# Add it to a pending list, and return
self.unconnected_sockets.append( (socket_object, mask) )
return
self._register_channel(socket_object, channel, mask)
except java.lang.Exception, jlx:
raise _map_exception(jlx)
...发布于 2014-05-27 14:15:24
对于某些应用程序,我在响应过程中遇到了setblocking(0)的问题。所以我还修改了jython/Lib/socket.py,如下所示:
jython/Lib/socket.py:
class _tcpsocket(_nonblocking_api_mixin):
...
def send(self, s):
try:
if not self.sock_impl: raise error(errno.ENOTCONN, 'Socket is not connected')
if self.sock_impl.jchannel.isConnectionPending():
self.sock_impl.jchannel.finishConnect()
numwritten = self.sock_impl.write(s)
# try again in blocking mode
if numwritten == 0 and self.mode == MODE_NONBLOCKING: # <-- line to add
try: self.setblocking(1) # <-- line to add
except: pass # <-- line to add
numwritten = self.sock_impl.write(s) # <-- line to add
if numwritten == 0 and self.mode == MODE_NONBLOCKING:
raise would_block_error()
return numwritten
except java.lang.Exception, jlx:
raise _map_exception(jlx)
... 我知道这两个更改都不是很“干净”,但这是让Jython作为CGIHTTPServer像Python一样工作的唯一方法。
https://stackoverflow.com/questions/23863415
复制相似问题