def main():
with contextlib.closing(socket.socket()) as s, \
contextlib.closing(s.makefile('wb',0)) as writer, \
contextlib.closing(s.makefile('rb', 0)) as reader:
# This will actually return a random testnet node
their_ip = socket.gethostbyname("testnet-seed.bitcoin.schildbach.de")
print("Connecting to:", their_ip)
my_ip = "127.0.0.1"
s.connect( (their_ip,PORT) )
stream = msg_stream(reader)
# Send Version packet
send(writer, version_pkt(my_ip, their_ip))
# Receive their Version
their_ver = next(stream)
print('Received:', their_ver)
# Send Version acknolwedgement (Verack)
send(writer, msg_verack())
# Fork off a handler, but keep a tee of the stream
stream = tee_and_handle(writer, stream)
# Get Verack
their_verack = next(stream)
# Send a ping!
try:
while True:
send(writer, msg_ping())
send(writer, msg_getaddr())
gevent.sleep(5)
except KeyboardInterrupt: pass
评论列表
文章目录