socket question(urgent!)

张少驰 zhangsc at neusoft.com
Sun Dec 16 20:38:07 EST 2001


   I listen UDP package without via normally way,the reason is that my firewall don't support adverse DNS,so the head of company ask me to collecting DNS packages from LAN,and extract ip and relevant domain from them,and if given IP,then it will return IP's relevant domain.My program of serpy.py is collecting DNS message and charge return IP's domain to client.py ,So I designed my program like follows,I'm a newbie and I don't know a lot of knowledge about network programming,I'm looking forward to receiving any help letter,Thanks! My email zhangsc at neusoft.com

server.py #It works at IP address of 10.1.1.27

import socket,struct
import os
import string
os.system("ifconfig eth0 promisc")
Ethhdr='!BBBBBBBBBBBBH'
Eth_len=struct.calcsize(Ethhdr)
IPhdr='!bbhhhbbhll'
IPhdr_len=struct.calcsize(IPhdr)
UDPhdr='!HHHH'
UDPhdr_len=struct.calcsize(UDPhdr)
ipsock=socket.socket(socket.AF_INET,10,8)
while 1:
  recvpack=ipsock.recv(1024)
  if len(recvpack) <= (Eth_len+IPhdr_len+UDPhdr_len):
     continue
  data=struct.unpack(Ethhdr,recvpack[:Eth_len])
  if data[12]==53:
    #collecting DNS package,and extract IP and IP's relevant domain
    .......
  if data[12]==2048:
    ipdata=struct.unpack(IPhdr,recvpack[Eth_len:Eth_len+IPhdr_len])
    if ipdata[6]==socket.IPPROTO_UDP:
      udpdata=struct.unpack(UDPhdr,recvpack[Eth_len+IPhdr_len:Eth_len+IPhdr_len+UDPhdr_len])
      if udpdata[1]==21567 or udpdata[0]==21567:
        #I get udpaddr from IP header of 32bit source ip address,
        #and I get udpdata[0] from UDP header of 16bit source port number.
        udpaddr="%i.%i.%i.%i" % (ipdata[8]>>24 & 255,ipdata[8]>>16 & 255,\
                                ipdata[8]>>8 & 255,ipdata[8] & 255)
        addr=udpaddr,udpdata[0]
        print type(addr)
        print addr 
        #for some program design reason,I can't use data,addr=udpSerSock.recvfrom(BUFSIZ) to get addr,
        #because data,addr=udpSerSock.recvfrom(BUFSIZ) is conflict with recvpack=ipsock.recv(1024)

        udpSerSock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
        data='relevant domant is',dbs[ip]
        udpSerSock.sendto(data,addr)
        udpSerSock.close() 

client.py #It works at IP address of 10.1.1.67

from socket import *
import string
HOST='10.1.1.27'
PORT=21567
BUFSIZ=1024
ADDR=(HOST,PORT)
udpCliSock=socket(AF_INET,SOCK_DGRAM)
data=raw_input('>')
udpCliSock.sendto(data,ADDR)
print udpCliSock.getsockname()
data, addr = udpCliSock.recvfrom(BUFSIZ)
if data:
  print data
udpCliSock.close()

First,I run server.py at 10.1.1.27
$python server.py

Then I run client.py at 10.1.1.67
$python client.py
>hello

The running result of server.py:
<type 'tuple'>
('10.1.1.67', 1165)
$

The running result of client.py,it raise a error:
('0.0.0.0', 1165)
Traceback (most recent call last):
  File "client.py", line 11, in ?
    data, addr = udpCliSock.recvfrom(BUFSIZ)
socket.error: (104, 'Connection reset by peer')
I know that my server.py don't use normal way to return data to client.py ,but I need this way! How to correct my programs? Any ideas will be appreciated!
      Thanks.
               Edward               
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20011217/14cb4240/attachment.html>


More information about the Python-list mailing list