[英]Python - If string in file starts with 16 then
I have a script that checks my ip address on my PC and writes it to a file on my PC - This is working fine 我有一个脚本可以检查PC上的IP地址并将其写入PC上的文件-正常工作
import socket
import sys
import requests
import urllib.request
import shutil
import subprocess
from time import sleep
import os
from os import system
# URL for download
URL = 'https://here/app.exe'
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 1))
local_ip_address = s.getsockname()[0]
sys.stdout = open("C:\\Temp\\nw_check.txt", "w+")
print(s.getsockname() [0])
Now I would like to do this next 现在我想下一步
Check the file C:\\Temp\\nw_check.txt and if the IP address in that file starts with 116, 115, 117 then download the above app using a proxy I will set .. If it starts with anything else then proceed with download 检查文件C:\\ Temp \\ nw_check.txt,如果该文件中的IP地址以116、115、117开头,则使用我将设置的代理下载上述应用程序。如果它以其他任何内容开头,则继续下载
if xxxxxx(xxxxxxxx()).startswith(('116', '115', '117')):
r = requests.get(URL, stream=True, proxies={'http': 'http://proxy:6547', 'https': 'http://proxy:6547'})
else:
r = requests.get(URL, stream=True)
sys.stdout = open("C:\\Temp\\nw_check.txt", "w+")
sys.stdout = open(“ C:\\ Temp \\ nw_check.txt”,“ w +”)
print(s.getsockname() [0])
打印(s.getsockname()[0])
Why are you setting the file to stdout then printing instead of just printing / writing directly to the file? 为什么将文件设置为stdout然后进行打印,而不仅仅是直接打印/写入文件?
with open("C:\\Temp\\nw_check.txt", "w+") as f:
print(s.getsockname()[0], file=f)
# of f.write(str(s.getsockname()[0])); f.write('\n')
Check the file C:\\Temp\\nw_check.txt and if the IP address in that file starts with 116, 115, 117
检查文件C:\\ Temp \\ nw_check.txt,并且该文件中的IP地址是否以116、115、117开头
Why are you going through an intermediate file? 为什么要浏览中间文件? Why not just check the result of
s.getsockname() [0]
directly? 为什么不直接检查
s.getsockname() [0]
的结果呢?
That aside: 1. if you're using the code above and are below the "with", or are in a different file, re-open the file in r
mode, read the first 3 characters and check if that's what you're looking for: 撇开:1.如果您使用的是上面的代码,并且在“ with”下面,或者在其他文件中,请以
r
模式重新打开该文件,读取前3个字符,然后检查这是否是您所需要的寻找:
with open("C:\\Temp\\nw_check.txt", "w+") as f:
print(s.getsockname()[0], file=f)
with open(fname, 'r') as f:
if f.read(3) in ('116', '115', '117'):
# etc…
with
body of the above, seek(0) to move the cursor back to the start of the file then read the first 3 characters and check. with
正文中,请执行seek(0)将光标移回文件的开头,然后读取前3个字符并进行检查。 sys.stdout.seek(0)
if f.read(3) in ('116', '115', '117'):
# etc…
just open the file again and read the ip address: 只需再次打开文件并读取IP地址即可:
with open("C:\\Temp\\nw_check.txt", "r") as ip_file:
ip_address = ip_file.readline()
if ip_address.startswith(("115", "116", "117")):
# and so on
Some variation of this to suit your needs should work: 可以满足您需要的一些变化形式应该起作用:
with open("C:\\Temp\\nw_check.txt", "r") as input_file:
for ip_address in input_file:
if ip_address.startswith(("115", "116", "117")):
r = requests.get(URL, stream=True, proxies={'http': 'http://proxy:6547', 'https': 'http://proxy:6547'})
else:
r = requests.get(URL, stream=True)
Since your file only has one line, the for loop will run just once and exit. 由于您的文件只有一行,因此for循环将只运行一次并退出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.