Mã trhe lấy adress IP từ TXT. Khi máy chủ trực tuyến viết trong tệp TXT mới. Nhưng mã này chỉ viết tệp cuối cùng trong tệp TXT. Và không bao giờ thời gian khác. tôi cần giúp đỡ
Tahnks
import os
file = open["IPs.txt","r+"]
with open["IPs.txt","r"] as file:
for line in file:
response = os.system["ping " + line]
if response == 0:
with open["IPsCheck.txt","w"] as file:
print[line]
file.write[line]
else:
print["server not available "]
HTSHAME
5.9354 Huy hiệu vàng30 Huy hiệu bạc52 Huy hiệu Đồng4 gold badges30 silver badges52 bronze badges
Đã hỏi ngày 7 tháng 3 năm 2019 lúc 10:51Mar 7, 2019 at 10:51
0
Bạn cần mở tệp đầu ra [ipscheck.txt] ở chế độ phụ lục: "A+"
Xem ở đây để biết thêm //www.guru99.com/reading-and-writing-files-in-python.html#2
Mã dưới đây dường như hoạt động. Thay đổi gỡ lỗi thành sai nếu bạn muốn đọc IPS từ tệp IPS.
import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
Đã trả lời ngày 7 tháng 3 năm 2019 lúc 10:55Mar 7, 2019 at 10:55
Baldermanbalermanbalderman
21.9k6 Huy hiệu vàng32 Huy hiệu bạc47 Huy hiệu đồng6 gold badges32 silver badges47 bronze badges
3
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
- Hãy cho chúng tôi xem cách trích xuất địa chỉ IP từ một tệp bằng Python. & NBSP;
- Thuật toán: & nbsp; & nbsp;
- Nhập mô -đun lại cho biểu thức chính quy.
- Mở tệp bằng hàm Open [].
r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'
- Đọc tất cả các dòng trong tập tin và lưu trữ chúng trong một danh sách.
- Tuyên bố mẫu cho địa chỉ IP. Mẫu regex là: & nbsp; & nbsp;
Đối với mọi yếu tố của tìm kiếm danh sách cho mẫu bằng hàm search [], hãy lưu trữ địa chỉ IP trong danh sách.
test.txt
python3
import
re
with
open
[
'C:/Users/user/Desktop/New Text Document.txt'
] as fh:
Hiển thị danh sách chứa địa chỉ IP.
Tệp được xử lý là Test.txt: & nbsp; & nbsp;
import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
0import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
1import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
2 import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
3import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
4import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
2 import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
6import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
7import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
8import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
9r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'0
import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
0r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'9
import
0import
1import
2import
3
Đầu ra:
Làm cách nào để trích xuất một địa chỉ IP từ một tệp văn bản trong Python?valid IP addresses.
Hãy cho chúng tôi xem cách trích xuất địa chỉ IP từ một tệp bằng Python ..
- Nhập mô -đun lại cho biểu thức chính quy ..
- Mở tệp bằng hàm Open [] ..
Đọc tất cả các dòng trong tệp và lưu trữ chúng trong danh sách ..
Tuyên bố mẫu cho địa chỉ IP. Mẫu regex là:.
Làm cách nào để trích xuất một địa chỉ IP từ một tệp văn bản?
Làm cách nào để trích xuất IP từ văn bản ?.
- 25 [0-5]-biểu thị số từ 250 đến 255
- 2 [0-4] [0-9]-đại diện cho số từ 200 đến 249
- [01]? [0-9] [0-9]?-đại diện cho các số từ 0 đến 199
Tệp sẽ được xử lý là test2.txt: & nbsp; & nbsp;
test2.txt
python3
import
re
with
open
[
import
9] as fh:
re
1re
2
import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
2 import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
3import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
4import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
2import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
6import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
7[
r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'0
with
1
import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
2r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'3
with
4
import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
2r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'3
r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'4
r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'5
r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'6
open
0open
1
r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'5
import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
2 open
4open
1open
6
import os
DEBUG = True
DEBUG_IPS = ['1.1.1.1', '8.8.8.8']
if DEBUG:
ips = DEBUG_IPS
else:
with open["IPs.txt", "r+"] as ips_file:
ips = [ip.strip[] for ip in ips_file.readlines[]]
with open["IPsCheck.txt", "w"] as available_ips_file:
for ip in ips:
response = os.system['ping {}'.format[ip]]
if response == 0:
available_ips_file.write[ip]
else:
print['server {} not available'.format[ip]]
2 open
8open
1[
0 [
1
[
2[
3
open
1[
5[
6
[
2[
8
import
2[
'C:/Users/user/Desktop/New Text Document.txt'
1
r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'0
import
2'C:/Users/user/Desktop/New Text Document.txt'
4
import
2[
'C:/Users/user/Desktop/New Text Document.txt'
7
r'[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}]'0
import
2] as fh:
0
Đầu ra: