Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Thursday, April 28, 2016

Modify binary file

It's been a while not writing anything because the my career changing to verify IC.
However, there is the little python script for modifying binary file byte by byte. The following code opens a file and seek to offset 0xAFF7 at the beginning. It writes 0xCC, 0xE2, 0x54, 0x7a into file.

import struct
magic = (0xCCE2, 0x547A)
with open("binary.bin", "r+b") as f:
    f.seek(0xAFF7)
    for m in magic:
        f.write(struct.pack('h', m))
    f.close()


Monday, January 07, 2008

Translate Decimal / IP dotted quad

Here are simple functions to translate decimal/ip dotted quad in python.
Usage:
>dot2dec("64.233.189.99")
1089060195L
>dec2dot(1089060195L)
'64.233.189.99'
>
import types
def dot2dec(ipForm, useHex = False):
if type(ipForm) == types.StringType:
ipf = ipForm.split(".")
elif type(ipForm) in (types.ListType, types.TupleType):
ipf = ipForm
elif type(ipForm) in (types.LongType, types.IntType):
return None
return reduce(lambda a,b: long(a)*256 + long(b), ipf)

def dec2dot(numbericIP):
if type(numbericIP) == types.StringType and not numbericIP.isdigit() :
return None
numIP = long(numbericIP)
return "%d.%d.%d.%d" % ((numIP>>24)&0xFF, (numIP>>16)&0xFF, (numIP>>8)&0xFF, numIP&0xFF)




FYI:
python google group

Sunday, September 24, 2006

Wednesday, March 01, 2006

Python: Funkload

FunkLoad:This tool enables to do functional and load testing of web appplication.

URL:
http://funkload.nuxeo.org/

Friday, February 24, 2006

Singleton using python under ms-windows

From ASPN python cookbook
Submitter: Dragan Jovelic

URL:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/474070
-------------------------------------------------------
from win32event import CreateMutex
from win32api import CloseHandle, GetLastError
from winerror import ERROR_ALREADY_EXISTS

class singleinstance:
""" Limits application to single instance """

def __init__(self):
self.mutexname = "testmutex_{D0E858DF-985E-4907-B7FB-8D732C3FC3B9}"
self.mutex = CreateMutex(None, False, self.mutexname)
self.lasterror = GetLastError()

def aleradyrunning(self):
return (self.lasterror == ERROR_ALREADY_EXISTS)

def __del__(self):
if self.mutex:
CloseHandle(self.mutex)


#---------------------------------------------#
# sample usage:
#

from singleinstance import singleinstance
from sys import exit

# do this at beginnig of your application
myapp = singleinstance()

# check is another instance of same program running
if myapp.aleradyrunning():
print "Another instance of this program is already running"
exit(0)

# not running, safe to continue...
print "No another instance is running, can continue here"

Friday, February 17, 2006

python websites

There are several good python-related websites. Here are below:
Python daily: http://www.pythonware.com/daily/
Dirtsimple: http://dirtsimple.org/
gmane.comp.python.announce: http://permalink.gmane.org/gmane.comp.python.announce

Tuesday, February 07, 2006

刪除某行

import os
ip = '192.168.1.123'
ip = ip.replace('.','\\.')
SourceFile = '/root/.ssh/known_hosts'
UpdateFile = '/root/.ssh/known_hosts'
params = (SourceFile, ip, UpdateFile)
result = os.system("cp %s /tmp/known_hosts.$$ ;awk '$1!~/^%s/ {print ; }' /tmp/known_hosts.$$ > %s;rm -rf /tmp/known_hosts.$$"% params) == 0