python-2.7

Extracting domain name from a DNS Response packet using dpkt library

僤鯓⒐⒋嵵緔 提交于 2021-01-27 07:41:46
问题 I'm trying to generate a list of all domain names and their corresponding IP addresses from a pcap file, using dpkt library available here My code is mostly based on this filename = raw_input('Type filename of pcap file (without extention): ') path = 'c:/temp/PcapParser/' + filename + '.pcap' f = open(path, 'rb') pcap = dpkt.pcap.Reader(f) for ts, buf in pcap: #make sure we are dealing with IP traffic try: eth = dpkt.ethernet.Ethernet(buf) except: continue if eth.type != 2048: continue #make

Convert numpy array to RGB img with custom pallete

北城余情 提交于 2021-01-27 07:41:17
问题 I want to convert a numpy.ndarray: out = [[12 12 12 ..., 12 12 12] [12 12 12 ..., 12 12 12] [12 12 12 ..., 12 12 12] ..., [11 11 11 ..., 10 10 10] [11 11 11 ..., 10 10 10] [11 11 11 ..., 10 10 10]] to RGB img. The colors are taken from an array: colors_pal = np.array( [0,0,128], #ind 0 [0,0,0], .... [255,255,255]], #ind 12 dtype=np.float32) So, for example, all the pixels with index 12 will be white (255,255,255). The way I do it now is very slow (about 1.5 sec/img): data = np.zeros((out

Convert numpy array to RGB img with custom pallete

江枫思渺然 提交于 2021-01-27 07:37:49
问题 I want to convert a numpy.ndarray: out = [[12 12 12 ..., 12 12 12] [12 12 12 ..., 12 12 12] [12 12 12 ..., 12 12 12] ..., [11 11 11 ..., 10 10 10] [11 11 11 ..., 10 10 10] [11 11 11 ..., 10 10 10]] to RGB img. The colors are taken from an array: colors_pal = np.array( [0,0,128], #ind 0 [0,0,0], .... [255,255,255]], #ind 12 dtype=np.float32) So, for example, all the pixels with index 12 will be white (255,255,255). The way I do it now is very slow (about 1.5 sec/img): data = np.zeros((out

Match length of two Python lists

五迷三道 提交于 2021-01-27 07:27:31
问题 I have two Python lists of different length. One may assume that one of the lists is multiple times larger than the other one. Both lists contain the same physical data but captured with different sample rates. My goal is to downsample the larger signal such that it has exactly as much data points as the smaller one. I came up with the following code which basically does the job but is neither very Pythonic nor capable of handling very large lists in a performant way: import math a = [1,2,3,4

Match length of two Python lists

泄露秘密 提交于 2021-01-27 07:25:39
问题 I have two Python lists of different length. One may assume that one of the lists is multiple times larger than the other one. Both lists contain the same physical data but captured with different sample rates. My goal is to downsample the larger signal such that it has exactly as much data points as the smaller one. I came up with the following code which basically does the job but is neither very Pythonic nor capable of handling very large lists in a performant way: import math a = [1,2,3,4

Why does this contextmanager behave differently with dict comprehensions?

拈花ヽ惹草 提交于 2021-01-27 07:21:50
问题 I have a context decorator that has side effects when it's done. I've noticed that the side effects don't occur if I use a dict comprehension. from contextlib import contextmanager import traceback import sys accumulated = [] @contextmanager def accumulate(s): try: yield finally: print("Appending %r to accumulated" % s) accumulated.append(s) def iterate_and_accumulate(iterable): for item in iterable: with accumulate(item): yield item def boom_unless_zero(i): if i > 0: raise RuntimeError("Boom

How do I import a module only when needed?

…衆ロ難τιáo~ 提交于 2021-01-27 07:10:41
问题 I am writing different python modules for gupshup, nexmo, redrabitt, etc. service providers. #gupshup.py class Gupshup(): def test(): print 'gupshup test' All the other modules have test() method with different content in them. I know whose test() to call. I want to write another module provider , which will look like - #provider.py def test(): #call test() from any of the providers I will pass some sting data as a command line argument which will have the name of the module. But I don't want

How do I import a module only when needed?

纵然是瞬间 提交于 2021-01-27 07:10:00
问题 I am writing different python modules for gupshup, nexmo, redrabitt, etc. service providers. #gupshup.py class Gupshup(): def test(): print 'gupshup test' All the other modules have test() method with different content in them. I know whose test() to call. I want to write another module provider , which will look like - #provider.py def test(): #call test() from any of the providers I will pass some sting data as a command line argument which will have the name of the module. But I don't want

^M characters in exported file, converting to newlines

大憨熊 提交于 2021-01-27 07:03:02
问题 I exported a CSV from excel to parse using python. When I opened the vimmed the CSV, I noticed that it was all one line with ^M characters where newlines should be. Name, Value, Value2, OtherStuff ^M Name, Value, Value2, OtherStuff ^M I have the file parsed such that I modify the values and put the into a string (using 'rU' mode in csvreader). However, the string has no newlines. So I am wondering, is there a way to split the string on this ^M character, or a way to replace it with a \n? 回答1:

^M characters in exported file, converting to newlines

為{幸葍}努か 提交于 2021-01-27 07:00:50
问题 I exported a CSV from excel to parse using python. When I opened the vimmed the CSV, I noticed that it was all one line with ^M characters where newlines should be. Name, Value, Value2, OtherStuff ^M Name, Value, Value2, OtherStuff ^M I have the file parsed such that I modify the values and put the into a string (using 'rU' mode in csvreader). However, the string has no newlines. So I am wondering, is there a way to split the string on this ^M character, or a way to replace it with a \n? 回答1: