Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #73419 > unrolled thread
| Started by | 不坏阿峰 <onlydebian@gmail.com> |
|---|---|
| First post | 2014-06-19 05:56 -0700 |
| Last post | 2014-06-25 03:53 -0700 |
| Articles | 8 — 5 participants |
Back to article view | Back to comp.lang.python
DHCP query script not work. 不坏阿峰 <onlydebian@gmail.com> - 2014-06-19 05:56 -0700
Re: DHCP query script not work. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-06-19 13:14 +0000
Re: DHCP query script not work. Peter Otten <__peter__@web.de> - 2014-06-19 15:23 +0200
Re: DHCP query script not work. soneedu@gmail.com - 2014-06-19 06:30 -0700
Re: DHCP query script not work. 不坏阿峰 <onlydebian@gmail.com> - 2014-06-19 06:32 -0700
Re: DHCP query script not work. Anssi Saari <as@sci.fi> - 2014-06-19 16:49 +0300
Re: DHCP query script not work. 不坏阿峰 <onlydebian@gmail.com> - 2014-06-19 06:56 -0700
Re: DHCP query script not work. 不坏阿峰 <onlydebian@gmail.com> - 2014-06-25 03:53 -0700
| From | 不坏阿峰 <onlydebian@gmail.com> |
|---|---|
| Date | 2014-06-19 05:56 -0700 |
| Subject | DHCP query script not work. |
| Message-ID | <1fe9debc-e3fc-4236-8669-31ba61683865@googlegroups.com> |
Dear all
i got code recipes from here. and i want to run it on win 7.
http://code.activestate.com/recipes/577649-dhcp-query/
i have do some modify and use print to check how it is work, but i am stucked now.
hope someone can help me. thanks a lot.
i meet this error:
Traceback (most recent call last):
File "D:/Workspace/TestExcel/Test/test_DHCP.py", line 138, in <module>
offer = DHCPOffer(data, discoverPacket.transactionID)
File "D:/Workspace/TestExcel/Test/test_DHCP.py", line 82, in __init__
self.unpack()
File "D:/Workspace/TestExcel/Test/test_DHCP.py", line 95, in unpack
dnsNB = int(data[268] / 4)
TypeError: unsupported operand type(s) for /: 'str' and 'int'
########################################
__author__ = 'Administrator'
'''
Created on Mar 27, 2011
@author: hassane
'''
import socket
import struct
from uuid import getnode as get_mac
from random import randint
def getMacInBytes():
print get_mac()
mac = str(hex(get_mac()))
print mac
mac = mac[2:]
mac = mac[:-1] # i edited
print mac, len(mac)
while len(mac) < 12:
mac = '0' + mac
print mac
macb = b''
for i in range(0, 12, 2):
print mac[i:i + 2]
m = int(mac[i:i + 2], 16)
#print m
macb += struct.pack('!B', m)
print repr(macb), struct.calcsize('!B'),"++"
print macb,"=="
return macb
class DHCPDiscover:
def __init__(self):
self.transactionID = b''
for i in range(4):
t = randint(0, 255)
self.transactionID += struct.pack('!B', t)
print self.transactionID, "=="
def buildPacket(self):
macb = getMacInBytes()
print repr(macb)
packet = b''
packet += b'\x01' # Message type: Boot Request (1)
packet += b'\x01' # Hardware type: Ethernet
packet += b'\x06' # Hardware address length: 6
packet += b'\x00' # Hops: 0
packet += self.transactionID # Transaction ID
packet += b'\x00\x00' # Seconds elapsed: 0
packet += b'\x80\x00' # Bootp flags: 0x8000 (Broadcast) + reserved flags
packet += b'\x00\x00\x00\x00' # Client IP address: 0.0.0.0
packet += b'\x00\x00\x00\x00' # Your (client) IP address: 0.0.0.0
packet += b'\x00\x00\x00\x00' # Next server IP address: 0.0.0.0
packet += b'\x00\x00\x00\x00' # Relay agent IP address: 0.0.0.0
# packet += b'\x00\x26\x9e\x04\x1e\x9b' #Client MAC address: 00:26:9e:04:1e:9b
packet += macb
packet += b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' #Client hardware address padding: 00000000000000000000
packet += b'\x00' * 67 #Server host name not given
packet += b'\x00' * 125 #Boot file name not given
packet += b'\x63\x82\x53\x63' #Magic cookie: DHCP
packet += b'\x35\x01\x01' #Option: (t=53,l=1) DHCP Message Type = DHCP Discover
#packet += b'\x3d\x06\x00\x26\x9e\x04\x1e\x9b' #Option: (t=61,l=6) Client identifier
packet += b'\x3d\x06' + macb
packet += b'\x37\x03\x03\x01\x06' #Option: (t=55,l=3) Parameter Request List
packet += b'\xff' #End Option
return packet
class DHCPOffer:
def __init__(self, data, transID):
self.data = data
self.transID = transID
self.offerIP = ''
self.nextServerIP = ''
self.DHCPServerIdentifier = ''
self.leaseTime = ''
self.router = ''
self.subnetMask = ''
self.DNS = []
self.unpack()
def unpack(self):
if self.data[4:8] == self.transID:
self.offerIP = '.'.join(map(lambda x: str(x), data[16:20]))
self.nextServerIP = '.'.join(map(lambda x: str(x), data[20:24])) # c'est une option
self.DHCPServerIdentifier = '.'.join(map(lambda x: str(x), data[245:249]))
self.leaseTime = str(struct.unpack('!L', data[251:255])[0])
self.router = '.'.join(map(lambda x: str(x), data[257:261]))
self.subnetMask = '.'.join(map(lambda x: str(x), data[263:267]))
#print self.router, self.subnetMask, self.leaseTime, self.DHCPServerIdentifier, repr(self.offerIP)
print repr(data)
print repr(data[268])
dnsNB = int(data[268] / 4)
for i in range(0, 4 * dnsNB, 4):
self.DNS.append('.'.join(map(lambda x: str(x), data[269 + i:269 + i + 4])))
def printOffer(self):
key = ['DHCP Server', 'Offered IP address', 'subnet mask', 'lease time (s)', 'default gateway']
val = [self.DHCPServerIdentifier, self.offerIP, self.subnetMask, self.leaseTime, self.router]
for i in range(4):
print('{0:20s} : {1:15s}'.format(key[i], val[i]))
print('{0:20s}'.format('DNS Servers') + ' : ', ) #end='' here also have error.
if self.DNS:
print('{0:15s}'.format(self.DNS[0]))
if len(self.DNS) > 1:
for i in range(1, len(self.DNS)):
print('{0:22s} {1:15s}'.format(' ', self.DNS[i]))
if __name__ == '__main__':
# defining the socket
dhcps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #internet, UDP
dhcps.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) #broadcast
try:
dhcps.bind(('', 68)) #we want to send from port 68
except Exception as e:
print('port 68 in use...')
dhcps.close()
input('press any key to quit...')
exit()
#buiding and sending the DHCPDiscover packet
discoverPacket = DHCPDiscover()
dhcps.sendto(discoverPacket.buildPacket(), ('<broadcast>', 67))
print('DHCP Discover sent waiting for reply...\n')
#receiving DHCPOffer packet
dhcps.settimeout(3)
try:
while True:
data = dhcps.recv(1024)
print "here",repr(discoverPacket.transactionID), ">>>", len(data)
offer = DHCPOffer(data, discoverPacket.transactionID)
if offer.offerIP:
offer.printOffer()
break
except socket.timeout as e:
print(e)
dhcps.close() #we close the socket
input('press any key to quit...')
exit()
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-06-19 13:14 +0000 |
| Message-ID | <53a2e23a$0$29988$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #73419 |
On Thu, 19 Jun 2014 05:56:57 -0700, 不坏阿峰 wrote: > Traceback (most recent call last): > File "D:/Workspace/TestExcel/Test/test_DHCP.py", line 138, in <module> > offer = DHCPOffer(data, discoverPacket.transactionID) > File "D:/Workspace/TestExcel/Test/test_DHCP.py", line 82, in __init__ > self.unpack() > File "D:/Workspace/TestExcel/Test/test_DHCP.py", line 95, in unpack > dnsNB = int(data[268] / 4) > TypeError: unsupported operand type(s) for /: 'str' and 'int' data[268] returns a string. You cannot divide a string by an int. Perhaps you need to change the line to this? dnsNB = int(data[268]) / 4 -- Steven D'Aprano
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2014-06-19 15:23 +0200 |
| Message-ID | <mailman.11149.1403184213.18130.python-list@python.org> |
| In reply to | #73419 |
不坏阿峰 wrote: > i got code recipes from here. and i want to run it on win 7. > http://code.activestate.com/recipes/577649-dhcp-query/ > > i have do some modify and use print to check how it is work, but i am > stucked now. > > hope someone can help me. thanks a lot. > > i meet this error: > > Traceback (most recent call last): > File "D:/Workspace/TestExcel/Test/test_DHCP.py", line 138, in <module> > offer = DHCPOffer(data, discoverPacket.transactionID) > File "D:/Workspace/TestExcel/Test/test_DHCP.py", line 82, in __init__ > self.unpack() > File "D:/Workspace/TestExcel/Test/test_DHCP.py", line 95, in unpack > dnsNB = int(data[268] / 4) > TypeError: unsupported operand type(s) for /: 'str' and 'int' The script is written for Python 3, and you seem to be using a Python 2 interpreter. While dnsNB = int(data[268]/4) would become dnsNB = ord(data[268])/4 in Python 2 that's probably not the only change that needs to be made. For someone not familiar with Python the easiest fix is to install Python 3.4 (you don't need to unistall Python 2) and to run the script as is.
[toc] | [prev] | [next] | [standalone]
| From | soneedu@gmail.com |
|---|---|
| Date | 2014-06-19 06:30 -0700 |
| Message-ID | <a9d903b8-a34e-426f-824a-6ee02525f17b@googlegroups.com> |
| In reply to | #73424 |
On Thursday, June 19, 2014 8:23:17 PM UTC+7, Peter Otten wrote: > 不坏阿峰 wrote: > > > > > i got code recipes from here. and i want to run it on win 7. > > > http://code.activestate.com/recipes/577649-dhcp-query/ > > > > > > i have do some modify and use print to check how it is work, but i am > > > stucked now. > > > > > > hope someone can help me. thanks a lot. > > > > > > i meet this error: > > > > > > Traceback (most recent call last): > > > File "D:/Workspace/TestExcel/Test/test_DHCP.py", line 138, in <module> > > > offer = DHCPOffer(data, discoverPacket.transactionID) > > > File "D:/Workspace/TestExcel/Test/test_DHCP.py", line 82, in __init__ > > > self.unpack() > > > File "D:/Workspace/TestExcel/Test/test_DHCP.py", line 95, in unpack > > > dnsNB = int(data[268] / 4) > > > TypeError: unsupported operand type(s) for /: 'str' and 'int' > > > > The script is written for Python 3, and you seem to be using a Python 2 > > interpreter. While > > > > dnsNB = int(data[268]/4) > > > > would become > > > > dnsNB = ord(data[268])/4 > > > > in Python 2 that's probably not the only change that needs to be made. For > > someone not familiar with Python the easiest fix is to install Python 3.4 > > (you don't need to unistall Python 2) and to run the script as is. yes, i use Python 2.7. i am a beginner learn network program part. i can not modify it myself now, i have trid some days. hope some expert can help me correct this code in Python 2.7. many thanks in advanced.
[toc] | [prev] | [next] | [standalone]
| From | 不坏阿峰 <onlydebian@gmail.com> |
|---|---|
| Date | 2014-06-19 06:32 -0700 |
| Message-ID | <b8b7485e-735d-4226-af83-c73b3c8874d8@googlegroups.com> |
| In reply to | #73424 |
在 2014年6月19日星期四UTC+7下午8时23分17秒,Peter Otten写道: > 不坏阿峰 wrote: > > > > > i got code recipes from here. and i want to run it on win 7. > > > http://code.activestate.com/recipes/577649-dhcp-query/ > > > > > > i have do some modify and use print to check how it is work, but i am > > > stucked now. > > > > > > hope someone can help me. thanks a lot. > > > > > > i meet this error: > > > > > > Traceback (most recent call last): > > > File "D:/Workspace/TestExcel/Test/test_DHCP.py", line 138, in <module> > > > offer = DHCPOffer(data, discoverPacket.transactionID) > > > File "D:/Workspace/TestExcel/Test/test_DHCP.py", line 82, in __init__ > > > self.unpack() > > > File "D:/Workspace/TestExcel/Test/test_DHCP.py", line 95, in unpack > > > dnsNB = int(data[268] / 4) > > > TypeError: unsupported operand type(s) for /: 'str' and 'int' > > > > The script is written for Python 3, and you seem to be using a Python 2 > > interpreter. While > > > > dnsNB = int(data[268]/4) > > > > would become > > > > dnsNB = ord(data[268])/4 > > > > in Python 2 that's probably not the only change that needs to be made. For > > someone not familiar with Python the easiest fix is to install Python 3.4 > > (you don't need to unistall Python 2) and to run the script as is. yes, i use Python 2.7. i am a beginner learn network program part. i can not modify it myself now, i have trid some days. hope some expert can help me correct this code in Python 2.7. many thanks in advanced.
[toc] | [prev] | [next] | [standalone]
| From | Anssi Saari <as@sci.fi> |
|---|---|
| Date | 2014-06-19 16:49 +0300 |
| Message-ID | <vg3ha3ht3fi.fsf@coffee.modeemi.fi> |
| In reply to | #73419 |
不坏阿峰 <onlydebian@gmail.com> writes: > Dear all > > i got code recipes from here. and i want to run it on win 7. > http://code.activestate.com/recipes/577649-dhcp-query/ It works for me as is in Windows 7. It's a Python 3 script though which might be your problem.
[toc] | [prev] | [next] | [standalone]
| From | 不坏阿峰 <onlydebian@gmail.com> |
|---|---|
| Date | 2014-06-19 06:56 -0700 |
| Message-ID | <07fe54f9-221f-4c8f-a72e-bebedaca17b4@googlegroups.com> |
| In reply to | #73429 |
On Thursday, June 19, 2014 8:49:21 PM UTC+7, Anssi Saari wrote: > 不坏阿峰 <onlydebian@gmail.com> writes: > > > > > Dear all > > > > > > i got code recipes from here. and i want to run it on win 7. > > > http://code.activestate.com/recipes/577649-dhcp-query/ > > > > It works for me as is in Windows 7. It's a Python 3 script though which > > might be your problem. i got that my issue is the Python version , my is 2.7. i am not familiar with 3.and my tools coded by 2.7. i am stucked on this script. tks for ur reply. hope have someone change this script work on 2.7
[toc] | [prev] | [next] | [standalone]
| From | 不坏阿峰 <onlydebian@gmail.com> |
|---|---|
| Date | 2014-06-25 03:53 -0700 |
| Message-ID | <d3766968-f014-4e42-887d-683da46d2de3@googlegroups.com> |
| In reply to | #73430 |
anyone can help ? 在 2014年6月19日星期四UTC+7下午8时56分06秒,不坏阿峰写道: > On Thursday, June 19, 2014 8:49:21 PM UTC+7, Anssi Saari wrote: > > > 不坏阿峰 <onlydebian@gmail.com> writes: > > > > > > > > > > > > > Dear all > > > > > > > > > > > > > > i got code recipes from here. and i want to run it on win 7. > > > > > > > http://code.activestate.com/recipes/577649-dhcp-query/ > > > > > > > > > > > > It works for me as is in Windows 7. It's a Python 3 script though which > > > > > > might be your problem. > > > > > > i got that my issue is the Python version , my is 2.7. i am not familiar with 3.and my tools coded by 2.7. > > > > i am stucked on this script. tks for ur reply. hope have someone change this script work on 2.7
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web