[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Python Sockets Help

Mark M Manning

3/10/2008 10:58:00 PM

I need your expertise with a sockets question.

Let me preface this by saying I don't have much experience with
sockets in general so this question may be simple.

I am playing with the mini dns server from a script I found online:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/491264...

All I want to do is edit this script so that it records the IP
address. I've seen other examples use the accept() object which
returns the data and the IP address it is receiving the data from. I
can't use that in this case but I'm wondering if someone could show me
how.

Here is the socket part of the script:

if __name__ == '__main__':
ip='192.168.1.1'
print 'pyminifakeDNS:: dom.query. 60 IN A %s' % ip

udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udps.bind(('',53))

try:
while 1:
data, addr = udps.recvfrom(1024)
p=DNSQuery(data)
udps.sendto(p.respuesta(ip), addr)
print 'Respuesta: %s -> %s' % (p.dominio, ip)
except KeyboardInterrupt:
print 'Finalizando'
udps.close()

Thanks to everyone in advance!
~Mark
3 Answers

Joe Riopel

3/10/2008 11:13:00 PM

0

On Mon, Mar 10, 2008 at 6:58 PM, Mark M Manning <mark.manning@gmail.com> wrote:
> I need your expertise with a sockets question.
>
> Let me preface this by saying I don't have much experience with
> sockets in general so this question may be simple.


I would suggest taking a quick look at this tutorial.
http://ilab.cs.byu.e...

Especially this:
http://ilab.cs.byu.e...socket/echoserver.html

Francesco Bochicchio

3/11/2008 8:03:00 AM

0

On 10 Mar, 23:58, Mark M Manning <mark.mann...@gmail.com> wrote:
> I need your expertise with a sockets question.
>
> Let me preface this by saying I don't have much experience with
> sockets in general so this question may be simple.
>
> I am playing with the mini dns server from a script I found online:http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/491264...
>
> All I want to do is edit this script so that it records the IP
> address.  I've seen other examples use the accept() object which
> returns the data and the IP address it is receiving the data from.  I
> can't use that in this case but I'm wondering if someone could show me
> how.
>
> Here is the socket part of the script:
>
> if __name__ == '__main__':
>   ip='192.168.1.1'
>   print 'pyminifakeDNS:: dom.query. 60 IN A %s' % ip
>
>   udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>   udps.bind(('',53))
>
>   try:
>     while 1:
>       data, addr = udps.recvfrom(1024)
>       p=DNSQuery(data)
>       udps.sendto(p.respuesta(ip), addr)
>       print 'Respuesta: %s -> %s' % (p.dominio, ip)
>   except KeyboardInterrupt:
>     print 'Finalizando'
>     udps.close()
>
> Thanks to everyone in advance!
> ~Mark

You already have the address of the sender, is in the 'addr' variable,
as returned by udps.recvfrom.
Change the print statement in sometinmh like:
print 'Respuesta (%s): %s -> %s' % ( addr, p.dominio, ip)
and you will see the sender address in dotted notation printed inside
the ().

Ciao
-------
FB

Mark M Manning

3/11/2008 1:08:00 PM

0

That's embarrassingly simple! Thank you very much!!



On Mar 11, 4:03 am, bock...@virgilio.it wrote:
> On 10 Mar, 23:58, Mark M Manning <mark.mann...@gmail.com> wrote:
>
>
>
> > I need your expertise with a sockets question.
>
> > Let me preface this by saying I don't have much experience with
> > sockets in general so this question may be simple.
>
> > I am playing with the mini dns server from a script I found online:http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/491264...
>
> > All I want to do is edit this script so that it records the IP
> > address. I've seen other examples use the accept() object which
> > returns the data and the IP address it is receiving the data from. I
> > can't use that in this case but I'm wondering if someone could show me
> > how.
>
> > Here is the socket part of the script:
>
> > if __name__ == '__main__':
> > ip='192.168.1.1'
> > print 'pyminifakeDNS:: dom.query. 60 IN A %s' % ip
>
> > udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
> > udps.bind(('',53))
>
> > try:
> > while 1:
> > data, addr = udps.recvfrom(1024)
> > p=DNSQuery(data)
> > udps.sendto(p.respuesta(ip), addr)
> > print 'Respuesta: %s -> %s' % (p.dominio, ip)
> > except KeyboardInterrupt:
> > print 'Finalizando'
> > udps.close()
>
> > Thanks to everyone in advance!
> > ~Mark
>
> You already have the address of the sender, is in the 'addr' variable,
> as returned by udps.recvfrom.
> Change the print statement in sometinmh like:
> print 'Respuesta (%s): %s -> %s' % ( addr, p.dominio, ip)
> and you will see the sender address in dotted notation printed inside
> the ().
>
> Ciao
> -------
> FB