简体   繁体   English

Python 3 在同一行上打印来自 try 语句的输出,我正在尝试学习线程

[英]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.

相关问题 我是 python 的新手,我想弄清楚如何在同一行上一次打印一个字母 - I'm new to python and I am trying to figure out how to print letters one at a time on the same line python——我正在尝试学习 python。 为什么我没有得到任何 output.? - python--am trying to learn python. Why do i not get any output.? 我正在尝试使用python脚本中的shell命令在目录中打印每个文件的最后一行 - I am trying to print the last line of every file in a directory using shell command from python script 尝试和除外。 我正在尝试实施评分,但继续获得除了打印作为输出 - Try and Except. I am trying to implement a grading score but keep on getting except print as an output Python:如何将打印语句和输入放在同一行? - Python: How do I put print statement and input on same line? Python:将try代码与try语句放在同一行有什么优势? - Python: Is there any advantage to placing try code on the same line as the try statement? 我正在尝试使用 python 从 json 文件中打印信息 - I am trying to print information from a json file with python 我正在尝试一次打印多行,每行在 python 2.7.11 中都有一个设定速度的延迟打印 - I am trying to print multiple lines at once, with each line having a delayed print of a set speed in python 2.7.11 尝试学习如何使用 python 制作 discord 机器人。 每次我尝试启动代码时都会出现相同的错误 - Trying to learn how to make a discord bot with python. Keep getting same error everytime I try to start the code 在同一行 python 中打印循环的 output? - Print output of loop in same line python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM