在Python脚本以截图的VNC您的会话出现问题问题,怎么解决

我想在web页面上加入VNC,有什么方法吗?后台使用的是python语言,有没有类似的python库。。关注我的订阅号“微wx笑”
在IE中使用Python作为开发脚本
在中使用作为开发脚本
目前支持的客户端脚本语言常见的有和,这两种脚本语言在操作页面元素方面基本能满足一般应用的需要,可通过和集成来获得扩展功能。
本文提供一种将作为客户端脚本语言的方法,利用的简洁和可扩展性使应用在客户端可实现更好的交互。也可用内置的网络功能,更容易地实现的功能。
根据在中使用的语法,可以猜到在中使用的语法是:
当执行这段代码时,需要调用的解释器来执行之间的代码,因此要让识别代码,就需要告诉在何处查找解释器。
这个功能在中已经实现,该扩展除客户端脚本支持外,还包括服务器端脚本、、支持。
首先从的老家下载一个安装包,按后安装到本地硬盘,假设安装在,并将该路径加到环境变量中。详细的安装参见手册。
然后到下载一个,它是一个安装包,安装时要求指定的安装目录,安装包会将一些文件安装到目录下。
切换到命令行模式,到目录目录下,运行 和,将注册到中,使能识别代码中的脚本。
下面是一个在中运行的代码,要运行它,只要保存在一个文件中,然后用打开,点击按钮,将显示次对话框。
因为的语法要求,必需从第一列开始,前面不能有空格或来缩进代码。
在中运行脚本虽可获得的强大功能,但要求所在机器必须安装解释器和。这限制了在一般应用上使用的可能性。但可以在以下的模式中使用。
客户端用作为运行容器,Python解释器和windows extension作为系统一部分安装在客户端,甚至有本地数据库,对数据库的操作在脚本中完成,需要和服务器端交互时,再用脚本来完成和服务器端的数据交换。这种模式集成了和的优点,客户端不需要安装专门的客户端软件,另外不需要时时连接到服务器,不需要管理等状态信息,可脱机完成业务后再连接服务器发送或获取数据。
相对于中用来实现网络功能,能提供更好更强的功能,并且编码更简洁。
Mark Hammond , Andy Robinson
Python Programming on Win32 , 2000
扫码向博主提问
有什么问题,我在这里等你
擅长领域:
没有更多推荐了,
不良信息举报
举报内容:
在IE中使用Python作为开发脚本
举报原因:
原文地址:
原因补充:
最多只允许输入30个字
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!如何将stdout重定向到 python 中的任意文件?评论时的编辑:长时间运行( 比如,网络应用程序) python 脚本启动时从ssh会话和backgounded内,而ssh会话已经关闭,应用程序便会引发发生io错误,不能完成时刻它试图写至标准输出。 我需要找到一种方法,让应用程序和模块输出到一个文件,而不是输出到一个文件,以防止由于错误而导致的错误。 使用 nohup,当前,我雇用 nohup curiosity,而出,将输出重定向到一个文件,而且该能够完成任务,但我想知道如果有一种方式可以做到 without.我已经尝试过了 sys.stdout = open('somefile', 'w') ,但这似乎无法阻止某些外部模块输出到终端( 或者 sys.stdout =.. . 线根本没有起火) 。 我知道它应该从我已经测试过的更简单的脚本中运行,但是我还没有时间在一个web应用程序上测试。
时间:17年05月05日原作者:
共14个回答
如果要在 python 脚本中进行重定向,请将 sys.stdout 设置为文件对象,这样做:import sys
sys.stdout = open('file', 'w')
print 'test'一个更常见的方法是在执行( 在 Windows 和Linux上相同) 时使用外壳重定向:$ python foo.py& file
你可以尝试得更好import sys
class Logger(object):
def __init__(self, filename="Default.log"):
self.terminal = sys.stdout
self.log = open(filename,"a")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
sys.stdout = Logger("yourlogfilename.txt")
print"Hello world!" # this is should be saved in yourlogfilename.txt
在 python 有 : contextlib.redirect_stdout() 函数from contextlib import redirect_stdout
with open('help.txt', 'w') as f:
with redirect_stdout(f):
print('it now prints to `help.text`')它类似于:import sys
from contextlib import contextmanager
@contextmanager
def redirect_stdout(new_target):
old_target, sys.stdout = sys.stdout, new_target # replace sys.stdout
yield new_target # run some code with the replaced stdout
sys.stdout = old_target # restore to the previous value可以用于早期的python 版本。 后一个版本不是 。 如果需要的话,它可以做成一个。它不会在文件描述符级别 e.g. 重定向 stdout:import os
from contextlib import redirect_stdout
stdout_fd = sys.stdout.fileno()
with open('output.txt', 'w') as f, redirect_stdout(f):
print('redirected to a file')
os.write(stdout_fd, b'not redirected')
os.system('echo this also is not redirected')b'not redirected' 和 'echo this also is not redirected' 未重定向到 output.txt 文件。要在文件描述符级别重定向,可以使用 os.dup2():import os
import sys
from contextlib import contextmanager
def fileno(file_or_fd):
fd = getattr(file_or_fd, 'fileno', lambda: file_or_fd)()
if not isinstance(fd, int):
raise ValueError("Expected a file (`.fileno()`) or a file descriptor")
@contextmanager
def stdout_redirected(to=os.devnull, stdout=None):
if stdout is None:
stdout = sys.stdout
stdout_fd = fileno(stdout)
# copy stdout_fd before it is overwritten
#NOTE: `copied` is inheritable on Windows when duplicating a standard stream
with os.fdopen(os.dup(stdout_fd), 'wb') as copied:
stdout.flush() # flush library buffers that dup2 knows nothing about
os.dup2(fileno(to), stdout_fd) # $ exec& &to
except ValueError: # filename
with open(to, 'wb') as to_file:
os.dup2(to_file.fileno(), stdout_fd) # $ exec& to
yield stdout # allow code to be run with the redirected stdout
# restore stdout to its previous value
#NOTE: dup2 makes stdout_fd inheritable unconditionally
stdout.flush()
os.dup2(copied.fileno(), stdout_fd) # $ exec& &copied如果使用 stdout_redirected() 而不是 redirect_stdout(),则使用相同的示例:import os
import sys
stdout_fd = sys.stdout.fileno()
with open('output.txt', 'w') as f, stdout_redirected(f):
print('redirected to a file')
os.write(stdout_fd, b'it is redirected nown')
os.system('echo this is also redirected')
print('this is goes back to stdout')从前输出的输出输出到 output.txt,只要 stdout_redirected() 上下文管理器处于活动状态。注意:stdout.flush() 不刷新 python 3上的缓冲区,其中i/o 直接在 read()/write() 系统调用上实现。 若要刷新所有打开的C stdio输出流,可以显式调用 libc.fflush(None),如果某些扩展使用 stdio-based i/o:try:
import ctypes
from ctypes.util import find_library
except ImportError:
libc = None
libc = ctypes.cdll.msvcrt # Windows
except OSError:
libc = ctypes.cdll.LoadLibrary(find_library('c'))
def flush(stream):
libc.fflush(None)
stream.flush()
except (AttributeError, ValueError, IOError):
pass # unsupported你可以使用 stdout 参数重定向其他流,而不仅仅是 sys.stdout 比如 来合并 sys.stderr 和 sys.stdout:def merged_stderr_stdout(): # $ exec 2&&1
return stdout_redirected(to=sys.stdout, stdout=sys.stderr)例如:from __future__ import print_function
import sys
with merged_stderr_stdout():
print('this is printed on stdout')
print('this is also printed on stdout', file=sys.stderr)注意:stdout_redirected() 混合缓冲的i/o ( sys.stdout 通常) 和无缓冲的i/o ( 直接对文件描述符进行操作) 。 注意,可能有要回答问题,你的编辑:你可以使用
来守护脚本并使用 logging 模块(
) 代替 print 语句,而仅仅重定向使用 nohup 运行的长时间运行的python 脚本。
另一个答案没有涵盖你希望分叉进程共享新的输出的情况。要这样做:from os import open, close, dup, O_WRONLY
old = dup(1)
open("file", O_WRONLY) # should open on 1
..... do stuff and then restore
dup(old) # should dup to 1
close(old) # get rid of left overs
( 添加了导入语句):暂时重定向 stdout:import sys
from contextlib import contextmanager
@contextmanager
def stdout_redirected(new_stdout):
save_stdout = sys.stdout
sys.stdout = new_stdout
yield None
sys.stdout = save_stdout如下所示:with opened(filename,"w") as f:
with stdout_redirected(f):
print"Hello world"当然,这不是 thread-safe,但也不是手工做同样的舞蹈。 在 single-threaded program ( 例如在脚本中) 中,它是一种流行的方法。
import sys
sys.stdout = open('stdout.txt', 'w')
用其他语言编写的程序( 比如 。 C ) 必须做特殊的魔术( 调用 double-forking ) 来脱离终端( 为了防止僵尸进程) 。 所以,我认为最好的解决方案是模拟它们。如果你的程序是 re-executing,你可以在command-line上选择重定向,比如 /usr/bin/python mycoolscript.py 2&&1 1&/dev/null有关详细信息,请参阅这里文章: ?
基于这个答案:,这是我在我的一个项目中发现的另一种方法。 对于替换 sys.stderr 或者 sys.stdout with,你必须确保替换符合 file interface,特别是如果这是你正在做的,因为stderr标准在不受控制的其他库中使用。 库可能正在使用其他文件对象方法。使用日志记录 python 设施检查按照这个方式,我仍然让所有东西都在坚持自己的标准错误/标准输出的消息到日志,并派( 或者任何有关该问题的文件) file:class FileToLogInterface(file):
Interface to make sure that everytime anything is written to stderr, it is
also forwarded to a file.
def __init__(self, *args, **kwargs):
if 'cfg' not in kwargs:
raise TypeError('argument cfg is required.')
if not isinstance(kwargs['cfg'], config.Config):
raise TypeError(
'argument cfg should be a valid '
'PostSegmentation configuration object i.e. '
'postsegmentation.config.Config')
self._cfg = kwargs['cfg']
kwargs.pop('cfg')
self._logger = logging.getlogger('access_log')
super(FileToLogInterface, self).__init__(*args, **kwargs)
def write(self, msg):
super(FileToLogInterface, self).write(msg)
self._logger.info(msg)
你需要一个终端多路复用器,比如和或者 我很惊讶的是,Ryan Amos对原始问题的评论只提到了一个解决方案,而不管 python 欺骗多么聪明,以及他们收到多少 upvotes 。 更多的关于ryan的评论,tmux是一个不错的替代GNU屏幕的方法。但原理是一样的: 如果你总是发现自己再确认离开一个终端作业正在运行,而你对咖啡店的一个三明治,弹到浴室,回家( 等等等) log-out,头然后以后,重新连接到你的终端会话的情况下从任何地方或者任何电脑终端多路复用器,就好像你从来没有人离开,都在 答案。 将它们看作是终端会话的VNC或者远程桌面。 还有什么是解决办法。 作为奖励,当老板和/或者合作伙伴进来,而你无意中 ctrl-w/cmd-w的终端窗口取代了你的浏览器窗口,你不会丢失最后 18 hours-worth的处理 !
Copyright (C) 2011 HelpLib All rights reserved. &&

我要回帖

更多关于 lol脚本荣誉截图 的文章

 

随机推荐