Python Hex to Ascii - windows

How can I make Python loop through a file and convert the hex to ascii? The problem is that when Python loops through the file it sees \n and does not parse the file.
import binascii
fo = open('test.doc', 'r')
print fo.readlines()
dataFormatHex = binascii.a2b_hex ("%s" %fo)
output = ""
for char in dataFormatHex:
if char in string.printable: output += char
else: output += "."
print "\n" + output
The error trace is:
Traceback (most recent call last):
File "C:\Python27\hex2ascii-looper.py", line 7, in <module>
dataFormatHex = binascii.a2b_hex ("%s" %fo)
TypeError: Non-hexadecimal digit found
Here is what is in the test.doc:
140000003700000027000000260000006D0000008E000000040000002E000000000000001D00000003000000410000004D0000005800000094000000880000005E0000004F00000040000000360000007B000000660000005A00000042000000300000002F0000004300000048000000050000000C0000004900000044000000220000002D000000180000003800000011000000010000001500000009000000640000008100000020000000700000006C00000087000000650000000F000000610000005D0000001F000000210000001900000079000000690000008A0000008D0000002A0000008C00000073000000330000003B000000680000006F000000800000000E000000020000004A0000005900000039000000740000001C00000012000000060000001E000000830000006B0000006A000000750000000A0000007A00000023000000550000007E000000340000001B0000001700000084000000520000007700000092000000470000009300000076000000710000004600000060000000720000002B000000530000001A000000900000007D00000016000000670000008B00000063000000450000002C00000035000000070000005B00000050000000910000002900000086000000310000003A0000000D0000007F000000560000002400000054000000890000006E00000085000000950000008F0000004C0000007800000062000000570000003D0000000B00000010000000130000002500000032000000820000004E0000005C0000003F000000080000004B000000280000005F0000003C000000510000003E000000FFFFFFFF
310032002D00310038002D00310034005F004D0065007800690063006F0020006D0061006300680069006E006500730020006F007500740020006F006600200064006100740065002D006E00650077002E0078006C00730078000000D00032000000000000000000000031322D31382D31345F4D657869636F206D616368696E6573206F7574206F6620646174652D6E65772E786C73782E6C6E6B00900008000400EFBE00000000000000002A00000000000000000000000000000000000000000000000000310032002D00310038002D00310034005F004D0065007800690063006F0020006D0061006300680069006E006500730020006F007500740020006F006600200064006100740065002D006E00650077002E0078006C00730078002E006C006E006B00000040000000
6300620074006E005F0070007900740068006F006E005F003000350020002D002000530074006100740065006D0065006E00740073002C00200044006F00630075006D0065006E0074006100740069006F006E002C00200061006E0064002000480065006C0070002E0066006C0076000000F2003200000000000000000000006362746E5F707974686F6E5F3035202D2053746174656D656E74732C20446F63756D656E746174696F6E2C20616E642048656C702E666C762E6C6E6B0000A60008000400EFBE00000000000000002A000000000000000000000000000000000000000000000000006300620074006E005F0070007900740068006F006E005F003000350020002D002000530074006100740065006D0065006E00740073002C00200044006F00630075006D0065006E0074006100740069006F006E002C00200061006E0064002000480065006C0070002E0066006C0076002E006C006E006B0000004C000000

Just use .strip() on your string, it will remove the trailing \n.
import binascii
f = open(yourFile)
for line in f.readlines():
print(binascii.a2b_hex("%s" % (line.strip()))
f.close()

Related

Serial Async Acquisition

I'ven't understood well the asyncio functionaly and I need a clarification.
I've checked that this code works:
from asyncio import get_event_loop
from serial_asyncio import open_serial_connection
async def main():
reader, writer = await open_serial_connection(url='COM10', baudrate=115200)
while True:
line = await reader.readline()
print(str(line, 'utf-8'))
asyncio.run(main())
basically the connection is established and I see messages coming from the serial port but it is not what I was expecting.
I'd like to start the acquisition from the serial and at the same time I'd like to go ahead doing other stuffs in my code.
Basically something like this one in scapy:
t = AsyncSniffer()
t.start()
print("Acquisition started")
wait (10)
t.stop()
I've also tried using the Thread option like this:
import serial
import threading
global stop_threads
import time
stop_threads=False
def thread_function_1():
ser = serial.Serial('COM1',115200)
while thread_function_1():
line=ser.readline()
if line:
string=line.decode()
print(string)
if stop_threads==true:
ser.close()
break
print ('print_1')
x= threading.Thread(name = "thread_function_1", target = thread_function_1)
x.start()
time.sleep(100)
stop_threads = True
print ('print_2')
x.join()
I'm getting this error which I cannot solve:
Traceback (most recent call last):
File "C:\Users\SIM\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\SIM\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\python\connessione_seriale.py", line 31, in thread_function_1
while thread_function_1():
File "C:\python\connessione_seriale.py", line 14, in thread_function_1
ser = serial.Serial('COM1',115200)
File "C:\Users\SIM\AppData\Local\Programs\Python\Python38\lib\site-packages\serial\serialwin32.py", line 33, in __init__
super(Serial, self).__init__(*args, **kwargs)
File "C:\Users\SIM\AppData\Local\Programs\Python\Python38\lib\site-packages\serial\serialutil.py", line 244, in __init__
self.open()
File "C:\Users\SIM\AppData\Local\Programs\Python\Python38\lib\site-packages\serial\serialwin32.py", line 64, in open
raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM1': PermissionError(13, 'Access is denied.', None, 5)
the COM1 is available.I've tried to open it via putty and it works.
this is the solution I've found. I've used a thread inside a class in order to start/stop the serial acquisition when I need it.
class ThreadTest:
def seriale(self, n):
ser = serial.Serial('COM9', 115200, timeout=1)
time.sleep(2)
with open('C:/python/test.txt', 'w') as output:
if self._running==False:
ser.close()
while self._running and n > 0:
line = ser.readline()
#print (line)
n -= 1
if line:
# Converting Byte Strings into unicode strings
string = line.decode()
#print (string)
output.write(string)
def terminate(self):
self._running = False
def __init__(self):
self._running = True
c = ThreadTest()
t1 = Thread(target=c.seriale, args=(10,)) #10 are the seconds of the acquisition via serial and can be replaced
t1.start() # start of the serial acquisition
time.sleep(10)# here the sleep can be replaced with code
c.terminate()

When streaming tweets with Python Twitter I get the error message: OpenSSL.SSL.SysCallError: (104, 'ECONNRESET')

I got this error where I have no clue where it come from and where to start to debug. I do not understand this error at all. I'll just post my code here. I utilized multithreading in my code to periodically save the tweets on separate files. My program will work fine for maybe over 10 days, and then throw me this error message. Maybe twitter reset my connection?
import twitter
import json
import time
from tqdm import tqdm
import string
from datetime import timedelta, datetime
import threading
import os
consumer_key = '*'
consumer_secret = '*'
access_token_key = '*'
access_token_secret = '*'
api = twitter.Api(consumer_key = consumer_key,\
consumer_secret = consumer_secret,\
access_token_key = access_token_key,\
access_token_secret = access_token_secret)
os.chdir('*/social_media/streamed_tweets')
#multiThreading class and functions starts here
class timerThread(threading.Thread):
def __init__(self, threadID, name, stoptime, stopevent): #remember to change args accordingly
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.stoptime = stoptime
self.stopevent = stopevent
def run(self):
print("Starting " + self.name)
twiTimer(self.stoptime, self.stopevent)
print("Exiting " + self.name)
class streamerThread(threading.Thread):
def __init__(self, threadID, name, keywords, stop): #remember to change args accordingly
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.keywords = keywords
self.stop = stop
def run(self):
print("Starting " + self.name)
twiStream(self.keywords, self.stop)
print("Exiting " + self.name)
def twiTimer(stop_time, stop_event):
while True:
now1 = datetime.now()
t = str(now1.timetz())
if t[:8] == stop_time: #make it t[:8] for daily loop
stop_event.set()
print('stop triggered')
time.sleep(1) #wait to pass the '00' time
time.sleep(0.1)
def twiStream(keywords, stop_event):
while True:
stream = api.GetStreamFilter(track = keywords, languages=['en'], filter_level = None)
now2 = datetime.now()
filename = str(now2.date())+'.txt' #change this to date() for daily loop
f = open(filename, 'w+')
stop_event.clear() #reset the event
print(now2)
with tqdm() as pbar:
while not stop_event.is_set():
counter = 2
for tweet in stream:
if counter <= 0:
break
f.write(json.dumps(tweet))
f.write('\n')
pbar.update()
counter -= 1
f.close()
time.sleep(0.5)
#multiThreading class and functions ends here
def main():
keywords = ['*']
stop = threading.Event()
stop_at = '00:00:00' #make it '00:00:00' for daily loop
#count = 10
thread1 = timerThread(1, 'timerThread', stop_at, stop)
thread2 = streamerThread(2, 'streamerThread', keywords, stop)
thread1.start()
thread2.start()
print("Exiting Main Thread")
if __name__ == "__main__":
main()
Here is the full error message:
Exception in thread streamerThread:
Traceback (most recent call last):
File "/home/abe/anaconda3/lib/python3.6/site-packages/urllib3/contrib/pyopenssl.py", line 285, in recv_into
return self.connection.recv_into(*args, **kwargs)
File "/home/abe/anaconda3/lib/python3.6/site-packages/OpenSSL/SSL.py", line 1814, in recv_into
self._raise_ssl_error(self._ssl, result)
File "/home/abe/anaconda3/lib/python3.6/site-packages/OpenSSL/SSL.py", line 1631, in _raise_ssl_error
raise SysCallError(errno, errorcode.get(errno))
OpenSSL.SSL.SysCallError: (104, 'ECONNRESET')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/abe/anaconda3/lib/python3.6/site-packages/urllib3/response.py", line 331, in _error_catcher
yield
File "/home/abe/anaconda3/lib/python3.6/site-packages/urllib3/response.py", line 637, in read_chunked
self._update_chunk_length()
File "/home/abe/anaconda3/lib/python3.6/site-packages/urllib3/response.py", line 569, in _update_chunk_length
line = self._fp.fp.readline()
File "/home/abe/anaconda3/lib/python3.6/socket.py", line 586, in readinto
return self._sock.recv_into(b)
File "/home/abe/anaconda3/lib/python3.6/site-packages/urllib3/contrib/pyopenssl.py", line 290, in recv_into
raise SocketError(str(e))
OSError: (104, 'ECONNRESET')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/abe/anaconda3/lib/python3.6/site-packages/requests/models.py", line 749, in generate
for chunk in self.raw.stream(chunk_size, decode_content=True):
File "/home/abe/anaconda3/lib/python3.6/site-packages/urllib3/response.py", line 461, in stream
for line in self.read_chunked(amt, decode_content=decode_content):
File "/home/abe/anaconda3/lib/python3.6/site-packages/urllib3/response.py", line 665, in read_chunked
self._original_response.close()
File "/home/abe/anaconda3/lib/python3.6/contextlib.py", line 99, in __exit__
self.gen.throw(type, value, traceback)
File "/home/abe/anaconda3/lib/python3.6/site-packages/urllib3/response.py", line 349, in _error_catcher
raise ProtocolError('Connection broken: %r' % e, e)
urllib3.exceptions.ProtocolError: ('Connection broken: OSError("(104, \'ECONNRESET\')",)', OSError("(104, 'ECONNRESET')",))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/abe/anaconda3/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "streamer.py", line 48, in run
twiStream(self.keywords, self.stop)
File "streamer.py", line 72, in twiStream
for tweet in stream:
File "/home/abe/anaconda3/lib/python3.6/site-packages/twitter/api.py", line 4575, in GetStreamFilter
for line in resp.iter_lines():
File "/home/abe/anaconda3/lib/python3.6/site-packages/requests/models.py", line 793, in iter_lines
for chunk in self.iter_content(chunk_size=chunk_size, decode_unicode=decode_unicode):
File "/home/abe/anaconda3/lib/python3.6/site-packages/requests/models.py", line 752, in generate
raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ('Connection broken: OSError("(104, \'ECONNRESET\')",)', OSError("(104, 'ECONNRESET')",))

Getting input for file copy in Python

I am trying to make a simple Python tool for copying all the contents from drive x to drive y, where it asks the user what the source and destination drives are.
Works great when I run it from inside Visual Studio, but when I try to run it via the command line (python.exe pythonapplication1.py), I get this error in the output:
What is your source drive letter?f
Traceback (most recent call last):
File "pythonapplication1.py", line 7, in <module>
inputSrc = input("What is your source drive letter?")
File "<string>", line 1, in <module>
NameError: name 'f' is not defined
Here is my code for this program:
import os
import sys
inputSrc ="x"
inputDest = "y"
inputSrc = input("What is your source drive
letter?")
inputDest = input("What is the destination drive
letter?")
src = inputSrc + ": "
dest = inputDest + ": "
copyCommand = "xcopy " + src + dest + "/s"
os.system(copyCommand)
loopCheck = "no"
while loopCheck == "no":
questionTest = input("Want to make another copy? y/n ")
if questionTest == "y":
input("Press any key once you put in the new blank drive.")
os.system(copyCommand)
if questionTest == "n":
loopCheck = "yes"
You should use raw_input("question...") instead of input("question...") in python2. This is because input will take the users input and execute it. This is why the interpreter complains about not knowing f.
You can use raw_input() instead of input()
i.e.
inputSrc = raw_input("What is your source drive
letter?")
inputDest = raw_input("What is the destination drive
letter?")
will work.

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte when reading an image

when l try to read an image as follow
with open('/home/image/482_PARTERRE_55682.jpg', 'r') as f:
imagebin=f.read()
l get the following error :
Traceback (most recent call last):
File "<input>", line 2, in <module>
File "/home/vincent/anaconda3/envs/my_env/lib/python3.5/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
l don't understand my error.
You should open image in binary mode i.e. "rb"
>>> from PIL import Image
>>> fh = open(".sample.jpg", 'rb')
>>> img = Image.open(fh)
with open('/home/image/482_PARTERRE_55682.jpg', 'rb') as f:
imagebin=f.read()

Python 3.4 Multiprocess throws TypeError("cannot serialize '_io.BufferedReader' object",)

Recently I wrote a multiprocess code in Python 3.4 to download some images, it's working blazingly fast at first, then I get the following error and cannot start the program anymore.
Traceback (most recent call last):
File "multiprocessing_d.py", line 23, in <module>
main()
File "multiprocessing_d.py", line 16, in main
p.map(download, lines)
File "/usr/local/lib/python3.4/multiprocessing/pool.py", line 260, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/usr/local/lib/python3.4/multiprocessing/pool.py", line 608, in get
raise self._value
multiprocessing.pool.MaybeEncodingError: Error sending result: '<multiprocessing.pool.ExceptionWithTraceback object at 0x7f1e047f32e8>'. Reason: 'TypeError("cannot serialize '_io.BufferedReader' object",)'
My code is as following
download_helper.py
import sys
import os
from pathlib import Path
url_prefix = r"Some prefix"
def setup_download_dir(dictionary):
download_dir = Path(dictionary)
if not download_dir.exists():
download_dir.mkdir()
return dictionary
def download_link(dictionary, line):
from urllib.request import urlretrieve
itemid = line.split()[0].decode()
link = line.split()[1].decode()
if (link.startswith("http")):
image_url = link
else:
image_url = url_prefix + link
if os.path.isfile(dictionary + "/" + itemid + ".jpg"):
#print("Already have " + itemid + ".jpg")
pass
else:
urlretrieve(image_url, dictionary + "/" + itemid + ".jpg")
multiprocessing_d.py
from functools import partial
from multiprocessing.pool import Pool
import sys
from time import time
from download_helper import setup_download_dir, download_link
def main():
file_path = sys.argv[1]
dic_path = sys.argv[2]
download_dir = setup_download_dir(dic_path)
download = partial(download_link, download_dir)
with open(file_path, 'rb') as f:
lines = f.readlines()
ts = time()
p = Pool(processes=16, maxtasksperchild=1)
p.map(download, lines)
p.close()
p.join()
print('Took {}s'.format(time() - ts))
f.close()
if __name__ == "__main__":
main()
I've tried to search online but didn't find much information useful. My suspect is that there might be some exception raised in urlretrieve, but I don't know how to debug it. Any comments or suggestions would be appreciated!!
James

Resources