简体   繁体   English

Python-如果文件中的字符串以16开头

[英]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…
  1. or 2 if you're using your original code or put that within the 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. 或2(如果您使用的是原始代码)或将其放在上面的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.

相关问题 如果Python中字符串以“*”结尾和开头 - If string ends and starts with "*" in Python 如何从巨大的文本文件(> 16GB)中提取特定行,其中每行以另一个输入文件中指定的字符串开头? - How to extract specific lines from a huge text file (>16GB) where each line starts with a string specified in another input file? 使用 for 循环查找文件差异以 bash 或 python 的特定字符串开头 - Find File differences using for loop starts with specific string with bash or python 获取以文件中的特定字符串开头的行,并将其与python中的其他字符串进行比较 - Getting a line that starts with a specific string in a file and compare it with another strings in python 用于UTF-16-LE文件的Python字符串替换 - Python string replace for UTF-16-LE file Python - 检查字符串是以“是”还是“否”开头? - Python - checking if a string starts with “Yes” or “No”? 在python 3中以十六进制为基数的字符串 - String to hex base 16 in python 3 Python:如果字符串以列表中的字符串开头 - Python: if string starts with a string from a list 在python中查找以希腊字母开头或结尾的字符串 - finding string that starts or ends with greek letters in python 如果换行符以句点开头,则拆分 python 字符串 - split python string if newline starts with a period
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM