[英]Python 3 Print output from try statement on the same line and I am trying to learn threading
import os
import time
import threading
import urllib.request
def message(msg):
print(time.strftime('[%H:%M:%S]'), msg)
def check(proxy):
proxy_support = urllib.request.ProxyHandler({'https':proxy})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
message("Trying => "+proxy)
try:
urllib.request.urlopen("https://www.google.com", timeout=5)
print("Working")
with open("CheckedProxies.txt", "a") as appe:
appe.write(proxy.replace("\n","") + "\n")
except:
print("Not Working")
pass
try:
proxies = open("/home/zion/Desktop/proxies.txt", "r").readlines()
except:
message("File Empty Exiting!")
exit()
if proxies == "":
print("File Empty, Enter Proxies")
newtxt = open("CheckedProxies.txt","w")
message("~ Loading "+ str(len(proxies)) +" Proxies!")
time.sleep(1)
for proxy in proxies:
check(proxy)
os.exit(CTRL-C)
message("Done Checking Proxies!")
I am trying to get the Not working to print on the same line as Trying proxy....my current output is:我正在尝试使 Not working to print 在与 Trying proxy... 相同的行上打印...我当前的输出是:
[23:20:51] ~ Loading 1598 Proxies! [23:20:51] ~ 加载 1598 个代理!
[23:20:52] Trying => 1.0.135.34:8080 [23:20:52] 尝试 => 1.0.135.34:8080
Not Working [23:20:53] Trying => 1.10.236.214:8080不工作 [23:20:53] 尝试 => 1.10.236.214:8080
Not Working [23:20:53] Trying => 103.122.255.18:8080不工作 [23:20:53] 尝试 => 103.122.255.18:8080
I am trying to get it to print like this我试图让它像这样打印
[23:20:53] Trying => 127.0.0.1:8080 Not Working! [23:20:53] 尝试 => 127.0.0.1:8080 不工作!
I have tried to "print("Not Working", end='')" but it prints out like this``我试过“打印(“不工作”,结束 =“')”但它打印出来像这样``
Not Working[23:07:30] Trying => 1.10.236.214:8080不工作 [23:07:30] 尝试 => 1.10.236.214:8080
I am not sure how to get the Not working to print after the trying and proxy....我不知道如何在尝试和代理后使无法打印....
I am also trying to learn how to use the threading module but am having trouble.... I want my program to open multiple threads testing my proxys... thank you in advance for any help.我也在尝试学习如何使用线程模块,但遇到了问题......我希望我的程序打开多个线程来测试我的代理......提前感谢您的帮助。
In regards to threading, the best way to do it here is to use a Multiprocessing Pool.关于线程,最好的方法是使用多处理池。
from multiprocessing import Pool
And then instead of:然后而不是:
for proxy in proxies:
check(proxy)
Use:用:
p=Pool(5)
p.map(check,proxies)
For making them all on one line, I would add the outputs to a string, then at the end of the function print it out.为了将它们全部放在一行中,我会将输出添加到一个字符串中,然后在函数的末尾将其打印出来。
Regarding the printing, it seems that you want to print without an automatic newline and you have worked out to use end
关于打印,您似乎想在没有自动换行符的情况下进行打印,并且您已经解决了使用end
print("Hello ", end = '')
print("World! ")
you say你说
I have tried to "print("Not Working", end='')" but it prints out like this``我试过“打印(“不工作”,结束 =“')”但它打印出来像这样``
but it seems to me that the problematic print() is in但在我看来,有问题的 print() 是在
def message(msg):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.