2012-01-28 3 views
-2

파이썬에서 작성할 수 있도록이 코드에서 정확히 무슨 일이 일어나는지 설명 할 수 있습니까? 내가 바로이 부분 때문에 나중에 사양을 작성 같은 C를 따라야합니다 Bep33을위한 파이썬 꽃 필터로 구현됩니다 내가파이썬에서 작성된 코드를 변환하는 방법

import hashlib 
import socket 
import struct 
class blommy(object): 
    def __init__(self): 
     self.bitarray= [0]*2048 

    def hashes(self,ip): 
     #convert decimal dotted quad string to long integer" 
     intip= struct.unpack('>L',socket.inet_aton(ip))[0] #>converting stringt to int 
     index = [0, 1] 
     hbyte = hashlib.sha1(intip) # # #sha1 doesnt accept int ? what needs to be done? 
     index[0] = ord(hbyte[0])| ord(hbyte[1])<< 8 
     index[1] = ord(hbyte[2])| ord(hbyte[3])<< 8 
     # how do i shift the bits? 
아래 주석을 참조 파이썬로 변환하려고

//fixed parameters 
k = 2 

m = 256*8 

//the filter 
byte[m/8] bloom ## 

function insertIP(byte[] ip) { 

byte[20] hash = sha1(ip) 

int index1 = hash[0] | hash[1] << 8 # how to in python? 
int index2 = hash[2] | hash[3] << 8 

// truncate index to m (11 bits required) 
index1 %= m ## ? 
index2 %= m ## ? 

// set bits at index1 and index2 
bloom[index1/8] |= 0x01 << index1 % 8 ## ?? 
bloom[index2/8] |= 0x01 << index2 % 8 ## ?? 
} 

// insert IP 192.168.1.1 into the filter: 
    insertIP(byte[4] {192,168,1,1}) 

토런트 DHT 비트

+2

거기에 C# 및 JavaScript 태그는 무엇입니까? – alexn

+0

@alexn probbaly 원래 코드는 C#가 아니라 C#입니다. 배열 형식의 구문 (비록 실제 C#도 아니지만) – wRAR

+0

원본 코드가 C#, C 등의 문제가 아니더라도 질문은 내가 DHT 용이라고 언급 한 논리를 이해하는 것이 었습니다. – Shazib

답변

2

sha1은 Python 2.x에서 일반 문자열을 허용합니다. socket.inet_aton (ip)에서 직접 개별 바이트가 필요할 수도 있습니다 (알고리즘의 순서가 맞는지 확실하지 않음)

비트 시프트에 대해서는 파이썬과 동일한 구문이 사용되지만 괄호를 추가해야 할 수도 있습니다 실행 명령에 대해 의심 스럽다.

또한 C char은 int로 작동 할 수 있지만 Python에서는 ord() 및 chr() 함수를 사용하여 항상 명시 적으로 변환해야합니다.

심지어 %=|= 같은 것들도 Python에서 작동합니다.

관련 문제