在python中创建线程(Creating Threads in python)
我有一个脚本,我想要一个函数与其他函数同时运行。
我看过的示例代码:
import threading def MyThread ( threading.thread ): doing something........ def MyThread2 ( threading.thread ): doing something........ MyThread().start() MyThread2().start()
我遇到麻烦让这个工作。 我宁愿使用线程函数而不是类来获取这个。
感谢任何帮助。
这是工作脚本,感谢所有的帮助。
class myClass(): def help(self): os.system('./ssh.py') def nope(self): a = [1,2,3,4,5,6,67,78] for i in a: print i sleep(1) if __name__ == "__main__": Yep = myClass() thread = Thread(target = Yep.help) thread2 = Thread(target = Yep.nope) thread.start() thread2.start() thread.join() print 'Finished'
I have a script and I want one function to run at the same time as the other.
The example code I have looked at:
import threading def MyThread ( threading.thread ): doing something........ def MyThread2 ( threading.thread ): doing something........ MyThread().start() MyThread2().start()
I am having trouble getting this working. I would prefer to get this going using a threaded function rather than a class.
This is the working script:
from threading import Thread class myClass(): def help(self): os.system('./ssh.py') def nope(self): a = [1,2,3,4,5,6,67,78] for i in a: print i sleep(1) if __name__ == "__main__": Yep = myClass() thread = Thread(target = Yep.help) thread2 = Thread(target = Yep.nope) thread.start() thread2.start() thread.join() print 'Finished'
原文:https://stackoverflow.com/questions/2905965
最满意答案
我已经找到了一个可在Chrome和Firefox中使用的解决方案。 我已经在用户手册中实现了代码, 不跟踪我的Google 。
演示(在Firefox 9和Chrome 17中测试): http : //jsfiddle.net/RxHw5/
推荐人隐藏Webkit(Chrome,..)和Firefox 37+(33 + *)
基于Webkit的浏览器(如Chrome,Safari) 支持
<a rel="noreferrer">
规格 。
引用者隐藏可以通过将此方法与两个事件侦听器组合来完全实现:
mousedown
- 点击,中间单击,右键单击上下文菜单,...keydown
( Tab Tab Tab ... Enter )。码:
function hideRefer(e) { var a = e.target; // The following line is used to deal with nested elements, // such as: <a href="."> Stack <em>Overflow</em> </a>. if (a && a.tagName !== 'A') a = a.parentNode; if (a && a.tagName === 'A') { a.rel = 'noreferrer'; } } window.addEventListener('mousedown', hideRefer, true); window.addEventListener('keydown', hideRefer, true);
* 33以后,
rel=noreferrer
是支持的,但支持仅限于页内链接。 当用户通过上下文菜单打开选项卡时,仍然会发送推荐人。 在Firefox 37 [ bug 1031264 ]中修复了这个错误。引荐来者隐藏旧版Firefox
火狐不支持
rel="noreferrer"
直到版本33` [ bug 530396 ] (或37,如果你想隐藏上下文菜单的引用者)。可以使用data-URI +
<meta http-equiv=refresh>
在Firefox(和IE)中隐藏引荐来源。 实现此功能更复杂,但也需要两个事件:
click
- 点击,中间单击, 输入- 上下文 菜单 - 右键单击Tab Tab ... 上下文菜单
在Firefox中,
click
事件将触发每个mouseup
并在链接(或表单控件)上按Enter 。contextmenu
事件是必需的,因为在这种情况下,click
事件触发时间太晚。基于数据URI和分秒秒超时:
触发click
事件时,href
属性将临时替换为数据URI。 事件完成,并且发生默认行为:打开数据URI,取决于target
属性和SHIFT / CTRL修饰符。
同时,href
属性恢复到原来的状态。当触发
contextmenu
事件时,链接也会分一秒钟更改。
- ...中的
Open Link in ...
选项将打开数据URI。Copy Link location
选项是指恢复的原始URI。- ☹
Bookmark
选项是指数据URI。- ☹
Save Link as
数据URI的点。码:
// Create a data-URI, redirection by <meta http-equiv=refresh content="0;url=.."> function doNotTrack(url) { // As short as possible. " can potentially break the <meta content> attribute, // # breaks the data-URI. So, escape both characters. var url = url.replace(/"/g,'%22').replace(/#/g,'%23'); // In case the server does not respond, or if one wants to bookmark the page, // also include an anchor. Strictly, only <meta ... > is needed. url = '<title>Redirect</title>' + '<a href="' +url+ '" style="color:blue">' +url+ '</a>' + '<meta http-equiv=refresh content="0;url=' +url+ '">'; return 'data:text/html,' + url; } function hideRefer(e) { var a = e.target; if (a && a.tagName !== 'A') a = a.parentNode; if (a && a.tagName === 'A') { if (e.type == 'contextmenu' || e.button < 2) { var realHref = a.href; // Remember original URI // Replaces href attribute with data-URI a.href = doNotTrack(a.href); // Restore the URI, as soon as possible setTimeout(function() {a.href = realHref;}, 4); } } } document.addEventListener('click', hideRefer, true); document.addEventListener('contextmenu', hideRefer, true);
结合两种方法
不幸的是,没有直接的方法来特征检测这个功能(更不用说帐户的错误)。 因此,您可以选择基于
navigator.userAgent
(即UA-sniffing)的相关代码,也可以使用其中一个复杂的检测方法, 如何检测rel =“noreferrer”支持? 。I have found a solution which works in Chrome and Firefox. I've implemented the code in a Userscript, Don't track me Google.
Demo (tested in Firefox 9 and Chrome 17): http://jsfiddle.net/RxHw5/
Referrer hiding for Webkit (Chrome, ..) and Firefox 37+ (33+*)
Webkit-based browsers (such as Chrome, Safari) support
<a rel="noreferrer">
spec.
Referrer hiding can fully be implemented by combining this method with two event listeners:
mousedown
- On click, middle-click, right-click contextmenu, ...keydown
(Tab Tab Tab ... Enter).Code:
function hideRefer(e) { var a = e.target; // The following line is used to deal with nested elements, // such as: <a href="."> Stack <em>Overflow</em> </a>. if (a && a.tagName !== 'A') a = a.parentNode; if (a && a.tagName === 'A') { a.rel = 'noreferrer'; } } window.addEventListener('mousedown', hideRefer, true); window.addEventListener('keydown', hideRefer, true);
*
rel=noreferrer
is supported in Firefox since 33, but support was limited to in-page links. Referrers were still sent when the user opened the tab via the context menu. This bug was fixed in Firefox 37 [bug 1031264].Referrer hiding for old Firefox versions
Firefox did not support
rel="noreferrer"
until version 33 `[bug 530396] (or 37, if you wish to hide the referrer for context menus as well).A data-URI +
<meta http-equiv=refresh>
can be used to hide the referrer in Firefox (and IE). Implementing this feature is more complicated, but also requires two events:
click
- On click, on middle-click, Entercontextmenu
- On right-click, Tab Tab ... ContextmenuIn Firefox, the
click
event is fired for eachmouseup
and hitting Enter on a link (or form control). Thecontextmenu
event is required, because theclick
event fires too late for this case.Based on data-URIs and split-second time-outs:
When theclick
event is triggered, thehref
attribute is temporarily replaced with a data-URI. The event finished, and the default behaviour occurs: Opening the data-URI, dependent on thetarget
attribute and SHIFT/CTRL modifiers.
Meanwhile, thehref
attribute is restored to its original state.When the
contextmenu
event is triggered, the link also changes for a split second.
- The
Open Link in ...
options will open the data-URI.- The
Copy Link location
option refers to the restored, original URI.- ☹ The
Bookmark
option refers to the data-URI.- ☹
Save Link as
points to the data-URI.Code:
// Create a data-URI, redirection by <meta http-equiv=refresh content="0;url=.."> function doNotTrack(url) { // As short as possible. " can potentially break the <meta content> attribute, // # breaks the data-URI. So, escape both characters. var url = url.replace(/"/g,'%22').replace(/#/g,'%23'); // In case the server does not respond, or if one wants to bookmark the page, // also include an anchor. Strictly, only <meta ... > is needed. url = '<title>Redirect</title>' + '<a href="' +url+ '" style="color:blue">' +url+ '</a>' + '<meta http-equiv=refresh content="0;url=' +url+ '">'; return 'data:text/html,' + url; } function hideRefer(e) { var a = e.target; if (a && a.tagName !== 'A') a = a.parentNode; if (a && a.tagName === 'A') { if (e.type == 'contextmenu' || e.button < 2) { var realHref = a.href; // Remember original URI // Replaces href attribute with data-URI a.href = doNotTrack(a.href); // Restore the URI, as soon as possible setTimeout(function() {a.href = realHref;}, 4); } } } document.addEventListener('click', hideRefer, true); document.addEventListener('contextmenu', hideRefer, true);
Combining both methods
Unfortunately, there is no straightforward way to feature-detect this feature (let alone account for bugs). So you can either select the relevant code based on
navigator.userAgent
(i.e. UA-sniffing), or use one of the convoluted detection methods from How can I detect rel="noreferrer" support?.
相关问答
更多-
检查JavaScript变量是否为空的最可靠方法是什么?(What's the most reliable way to check if a JavaScript variable is null?)[2021-06-10]
以上都不是。 您不希望使用==或其中的一种,因为它执行类型强制 。 如果你真的想检查某些东西是否显式为空,请使用===运算符。 再次,你的问题可能表明你的要求可能有些不明确。 你是否确切地指null ; 或者还有undefined计数? myVar === null肯定会告诉你变量是否为null,这是你问的问题,但这真的是你想要的吗? 请注意,在这个SO问题中有更多的信息。 这不是直接重复的,但它涵盖了非常相似的原则。 None of the above. You don't want to use ==, ... -
在JavaScript中隐藏/欺骗引荐来源的最可靠的方法是什么?(What is the most reliable way to hide / spoof the referrer in JavaScript?)[2023-07-04]
我已经找到了一个可在Chrome和Firefox中使用的解决方案。 我已经在用户手册中实现了代码, 不跟踪我的Google 。 演示(在Firefox 9和Chrome 17中测试): http : //jsfiddle.net/RxHw5/ 推荐人隐藏Webkit(Chrome,..)和Firefox 37+(33 + *) 基于Webkit的浏览器(如Chrome,Safari) 支持 规格 。 引用者隐藏可以通过将此方法与两个事件侦听器组合来完全实现: mou ... -
如何使用JavaScript获取引荐来源的域/主机名?(How do I get the referrer's domain/host name using JavaScript?)[2023-07-08]
通过解析它 document.referrer.split( '/' ); 会让你靠近 或者看看这个 http://blog.stevenlevithan.com/archives/parseuri 如果引荐来自浏览器,这将是理智的 - 但是为了防止您想要更强大的解析。 By parsing it. document.referrer.split( '/' ); will get you close. Or take a look at this http://blog.stevenlevithan.com ... -
您可以使用当前请求的UrlReferrer属性: Request.UrlReferrer 这将从请求中读取Referer HTTP头,客户端(用户代理)可能提供也可能不提供。 You could use the UrlReferrer property of the current request: Request.UrlReferrer This will read the Referer HTTP header from the request which may or may not be sup ...
-
如何欺骗网站推荐人?(How to spoof site referrer?)[2021-11-24]
http referrer是一个请求头。 服务器(PHP)只能设置响应标题。 因此欺骗引用者的唯一方法是客户端通常通过某种浏览器插件来执行此操作。 The http referrer is a request header. The server (PHP) can only set response headers. Thus the only way to spoof a referrer is for the client to do it, usually via some sort of brow ... -
document.referrer不是URL编码我最近在所有主流浏览器中测试过它,并且它在每个浏览器中都显示为非编码。 My final answer for this question: Referer may/may not be encoded! With my tests some (only some!) IEs send unencoded referrers (I guess it depends on the locale of client system)
-
当您从https://www.google.com进行搜索时,您从我们的自然搜索列表中访问的网站仍然会知道您来自Google,但不会收到有关每个查询的信息。 请阅读: http : //googleblog.blogspot.com/2011/10/making-search-more-secure.html When you search from https://www.google.com, websites you visit from our organic search listings wil ...
-
可以使用JavaScript + Cookies欺骗会话吗?(Is is possible to spoof a session with JavaScript + Cookies?)[2021-11-25]
是的他们可以。 这家伙似乎有一篇文章概述了例子: http : //skeptikal.org/2009/11/cross-subdomain-cookie-attacks.html 您可以设置cookie的域以防止这种情况发生。 它设置为;domain=...在cookie中,您的给定语言可能有直接执行此操作的工具。 Yes, they can. This guy appears to have an article outlining examples: http://skeptikal.org/200 ... -
Javascript路径名引用者(Javascript pathname referrer)[2023-07-06]
不,您只能手动提取所需的部分: document.referrer.replace(/^[^:]+:\/\/[^/]+/, '').replace(/#.*/, '') No, you can only extract needed part manually: document.referrer.replace(/^[^:]+:\/\/[^/]+/, '').replace(/#.*/, '') -
在HTML中链接Referrer(Link Referrer in HTML)[2022-03-03]
我认为元刷新不会留下引用者的痕迹这里的事情是从它获取它一个onclick( a )元素,所以你应该 ,AFAIK,能够做类似的事情: Click me function mask(url) { var meta = document.createElement("met ...