在本文中,我们将带你了解如何在Python中进行DNS查找,包括引用/etc/hosts?在这篇文章中,我们将为您详细介绍如何在Python中进行DNS查找,包括引用/etc/hosts?的方方面面,
在本文中,我们将带你了解如何在Python中进行DNS查找,包括引用/ etc / hosts?在这篇文章中,我们将为您详细介绍如何在Python中进行DNS查找,包括引用/ etc / hosts?的方方面面,并解答python dns查询常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的iphone – 如何在iOS上执行DNS查询、Python-如何在Python中进行相对导入?、[python省时间]处理文档,包括批量查找,替换,、准备数据如何在python中进行深度学习。
本文目录一览:- 如何在Python中进行DNS查找,包括引用/ etc / hosts?(python dns查询)
- iphone – 如何在iOS上执行DNS查询
- Python-如何在Python中进行相对导入?
- [python省时间]处理文档,包括批量查找,替换,
- 准备数据如何在python中进行深度学习
如何在Python中进行DNS查找,包括引用/ etc / hosts?(python dns查询)
dnspython会很好地完成我的DNS查找,但它完全忽略的内容/etc/hosts
。
是否有一个python库调用将做正确的事情?即首先检查etc/hosts
,否则仅回退到DNS查找?
答案1
小编典典我真的不知道,如果你想要做的DNS查找 自己 或者如果你只是想要一台主机的IP地址。如果您想要后者,
import socketprint(socket.gethostbyname(''localhost'')) # result from hosts fileprint(socket.gethostbyname(''google.com'')) # your os sends out a dns query
iphone – 如何在iOS上执行DNS查询
thanx提前
部分来自其他片段,我发现这个代码
Boolean result; CFHostRef hostRef; NSArray *addresses; Nsstring *hostname = @"apple.com"; hostRef = CFHostCreateWithName(kcfAllocatorDefault,(CFStringRef)hostname); if (hostRef) { result = CFHostStartInfoResolution(hostRef,kcfHostAddresses,NULL); // pass an error instead of NULL here to find out why it Failed if (result == TRUE) { addresses = (NSArray*)CFHostGetAddressing(hostRef,&result); } } if (result == TRUE) { [addresses enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop) { Nsstring *strDNS = [Nsstring stringWithUTF8String:inet_ntoa(*((struct in_addr *)obj))]; NSLog(@"Resolved %d->%@",idx,strDNS); }]; } else { NSLog(@"Not resolved"); }
但这是为每个主机生成相同的IP解决0-> 220.120.64.1任何帮助?
解决方法
if (result == TRUE) { NSMutableArray *tempDNS = [[NSMutableArray alloc] init]; for(int i = 0; i < CFArrayGetCount(addresses); i++){ struct sockaddr_in* remoteAddr; CFDataRef saData = (CFDataRef)CFArrayGetValueAtIndex(addresses,i); remoteAddr = (struct sockaddr_in*)CFDataGetBytePtr(saData); if(remoteAddr != NULL){ // Extract the ip address //const char *strIP41 = inet_ntoa(remoteAddr->sin_addr); Nsstring *strDNS =[Nsstring stringWithCString:inet_ntoa(remoteAddr->sin_addr) encoding:NSASCIIStringEncoding]; NSLog(@"RESOLVED %d:<%@>",i,strDNS); [tempDNS addobject:strDNS]; } } }
Python-如何在Python中进行相对导入?
想象一下这个目录结构:
app/
__init__.py
sub1/
__init__.py
mod1.py
sub2/
__init__.py
mod2.py
我正在编码mod1
,我需要从中导入一些东西mod2
。我该怎么办?
我尝试过,from ..sub2 import mod2
但是得到了“未打包的相对导入尝试”。
我四处搜寻,但只发现"sys.path manipulation"
骇客。有没有一种干净的方法?
[python省时间]处理文档,包括批量查找,替换,
1、批量查找替换
# -*- coding: utf-8 -*-
import os
import re
# path=os.getcwd()
str_old = ''insert''
str_new = ''frs.event.queue''
file_formate = ''init.sql''
file_sql=open(r''F:\bak\init_all.sql'', ''r+'', encoding=''utf-8'')
def replace_txt(path):
if path.find(file_formate) == len(path) - len(file_formate):
with open(path, ''r+'', encoding=''utf-8'') as file:
str = file.read()
if str.find(str_old) > 0:
print(path)
str = str.replace(str_old, str_new)
print(str)
file.seek(0, 0)
file.write(str)
# file.close()
# replace_txt(r''D:\python\workspace\tools\util\txt12\test.txt.py'')
def find_txt(path):
with open(path, ''r+'', encoding=''utf-8'') as file:
i=0
while True:
line = file.readline()
i=i+1
if not line:
break
else:
try:
str_line = str(line)
# if path.find(str_old) > 0:
if str_line.find(str_old) >=0:
print(str_line)
break
except:
print(str(line))
2、如果不知道文件是否utf-8
def find_txt_no_formate(path):
if file_formate !='''':
if path.find(file_formate) == len(path) - len(file_formate):
print(path)
with open(path, ''r+'', encoding=''utf-8'') as file:
str = file.read()
file_sql.write(''\n--------------------------------'')
file_sql.write(path)
file_sql.write(str)
# find_txt(path)
else:
find_txt(path)
3、列出所有文件
def list_all_file(rootdir):
files = []
list = os.listdir(rootdir)
for i in range(0, len(list)):
path = os.path.join(rootdir, list[i])
if os.path.isdir(path):
files.extend(list_all_file(path))
if os.path.isfile(path):
files.append(path)
try:
find_txt_no_formate(path)
except:
print("e")
return files
准备数据如何在python中进行深度学习
在每行上方添加注释以说明其目的:
#input is a 2D dataframe of images
def data_prep(raw):
#convert the classes in raw to a binary matrix
#also known as one hot encoding and is typically done in ML
out_y = keras.utils.to_categorical(raw.label,num_classes)
#first dimension of raw is the number of images; each row in the df represents an image
num_images = raw.shape[0]
#remove the first column in each row which is likely a header and convert the rest into an array of values
#ML algorithms usually do not take in a pandas dataframe
x_as_array = raw.values[:,1:]
#reshape the images into 3 dimensional
#1st dim: number of images
#2nd dim: height of each image (i.e. rows when represented as an array)
#3rd dim: width of each image (i.e. columns when represented as an array)
#4th dim: the number of pixels which is 3 (RGB) for colored images and 1 for gray-scale images
x_shaped_array = x_as_array.reshape(num_images,img_rows,img_cols,1)
#this normalizes (i.e. 0-1) the image pixels since they range from 1-255.
out_x = x_shaped_array / 255
return out_x,out_y
要处理彩色图像,数组中的第4维应为3,代表RGB values。请查看此tutorial,以获取有关CNN及其输入的更多详细信息。
今天关于如何在Python中进行DNS查找,包括引用/ etc / hosts?和python dns查询的讲解已经结束,谢谢您的阅读,如果想了解更多关于iphone – 如何在iOS上执行DNS查询、Python-如何在Python中进行相对导入?、[python省时间]处理文档,包括批量查找,替换,、准备数据如何在python中进行深度学习的相关知识,请在本站搜索。
本文标签: