佳音的博客

2008/01/06

一段 http 上传 文件的代码

Filed under: Uncategorized — 佳音 @ 5:31 下午

来自 :http://www.opentle.org/th/node/6021http://wiki.forum.nokia.com/index.php/How_to_upload_a_file_to_server_with_multipart/form-data

## HTTP file uploader
class HTTPFileUploader:

2008/01/04

http 伪造ip

Filed under: Uncategorized — 佳音 @ 10:30 上午

在http协议数据头里面加入选项“x-forward-for”,例如:“x-forward-for:202.204.76.254”,这样发送出去的包,就是一个特殊的包,在收包方看来,这个包的意思是,一个代理服务器发过来的数据包,而这个包的真是ip是“202.204.76.254”,其实还是实现的是三次握手,但是只不过是在发包的同时,对收包方提到了一个第三者。
function get_ip(){
if(getenv(‘HTTP_CLIENT_IP’)) {
$gb_ip = getenv(‘HTTP_CLIENT_IP’);
} elseif(getenv(‘HTTP_X_FORWARDED_FOR’)) {
$gb_ip = getenv(‘HTTP_X_FORWARDED_FOR’);
} elseif(getenv(‘REMOTE_ADDR’)) {
$gb_ip = getenv(‘REMOTE_ADDR’);
} else {
$gb_ip = $_SERVER['REMOTE_ADDR'];
}
return $gb_ip;
}

]]>

2008/01/03

mini dns服务器

Filed under: Uncategorized — 佳音 @ 10:35 上午

1、

  1.  
  2. #!/user/bin/python
  3. #-*- coding: utf-8 -*-
  4. import socket
  5.  
  6. class DNSQuery:
  7. def __init__(self, data):
  8.     self.data=data
  9.     self.dominio=
  10.     tipo = (ord(data[2]) >> 3) & 15   # Opcode bits
  11.     if tipo == 0:                     # Standard query
  12.       ini=12
  13.       lon=ord(data[ini])
  14.       while lon != 0:
  15.         self.dominio+=data[ini+1:ini+lon+1]+‘.’
  16.         ini+=lon+1
  17.         lon=ord(data[ini])
  18.  
  19. def respuesta(self, ip):
  20.     packet=
  21.     if self.dominio:
  22.       packet+=self.data[:2] + "\x81\x80"
  23.       packet+=self.data[4:6] + self.data[4:6] + \x00\x00\x00\x00′   # Questions and Answers Counts
  24.       packet+=self.data[12:]                                         # Original Domain Name Question
  25.       packet+=\xc0\x0c’                                             # Pointer to domain name
  26.       packet+=\x00\x01\x00\x01\x00\x00\x00\x3c\x00\x04′             # Response type, ttl and resource data length -> 4 bytes
  27.       packet+=str.join(,map(lambda x: chr(int(x)), ip.split(‘.’))) # 4bytes of IP
  28.     return packet
  29.  
  30. if __name__ == ‘__main__’:
  31. udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  32. udps.bind((,53))
  33. try:
  34.     while 1:
  35.       try:
  36.         data, addr = udps.recvfrom(1024)
  37.         p=DNSQuery(data)
  38.         ip=socket.gethostbyname(p.dominio)
  39.       except socket.error, e:
  40.           continue
  41.           print "Error Socket.error %s" % e
  42.       udps.sendto(p.respuesta(ip), addr)
  43.       print ‘Respuesta: %s -> %s’ % (p.dominio, ip)
  44. except KeyboardInterrupt:
  45.     print ‘Finalizando’
  46.     udps.close()
  47.  
  48.  

2、

  1.  
  2. #!/user/bin/python
  3. #-*- coding: utf-8 -*-
  4. import socket
  5. import threading
  6. class Prce(threading.Thread):
  7.     def __init__(self,data , addr, sock):
  8.         threading.Thread.__init__(self)
  9.         self.data = data
  10.         self.addr = addr
  11.         self.sock = sock
  12.     def run(self):
  13.         p = DNSQuery(self.data)
  14.         ip = socket.gethostbyname(p.dominio)
  15.         self.sock.sendto(p.respuesta(ip), self.addr)
  16.         print ‘Respuesta: %s -> %s’ % (p.dominio, ip)
  17. class DNSQuery:
  18. def __init__(self, data):
  19.     self.data=data
  20.     self.dominio=
  21.     tipo = (ord(data[2]) >> 3) & 15   # Opcode bits
  22.     if tipo == 0:                     # Standard query
  23.       ini=12
  24.       lon=ord(data[ini])
  25.       while lon != 0:
  26.         self.dominio+=data[ini+1:ini+lon+1]+‘.’
  27.         ini+=lon+1
  28.         lon=ord(data[ini])
  29.  
  30. def respuesta(self, ip):
  31.     packet=
  32.     if self.dominio:
  33.       packet+=self.data[:2] + "\x81\x80"
  34.       packet+=self.data[4:6] + self.data[4:6] + \x00\x00\x00\x00′   # Questions and Answers Counts
  35.       packet+=self.data[12:]                                         # Original Domain Name Question
  36.       packet+=\xc0\x0c’                                             # Pointer to domain name
  37.       packet+=\x00\x01\x00\x01\x00\x00\x00\x3c\x00\x04′             # Response type, ttl and resource data length -> 4 bytes
  38.       packet+=str.join(,map(lambda x: chr(int(x)), ip.split(‘.’))) # 4bytes of IP
  39.     return packet
  40.  
  41. if __name__ == ‘__main__’:
  42. udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  43. udps.bind((,53))
  44. try:
  45.     while 1:
  46.       try:
  47.         data, addr = udps.recvfrom(1024)
  48.         tmp = Prce(data,addr,udps)
  49.         tmp.start()
  50.         tmp.join()
  51.       except socket.error, e:
  52.           continue
  53. except error:
  54.     print ‘Finalizando’
  55.     udps.close()
  56. udps.close()
  57.  

2008/01/02

简单爬虫

Filed under: Uncategorized — 佳音 @ 7:37 下午

class Crawl{
/**
* @author jiayin
* @desc 简单爬虫游戏
* @param $store 为了方便 爬取以后 集中存储
* @method static public store() 边处理边存储
**/
public static $flag = true;
public static $store;
public static function getLink($content){

匹配图片url的正则

Filed under: Uncategorized — 佳音 @ 6:50 下午

preg_match_all(‘#]*src\s*=\s*[\'|\"]?((?:http://)?[^\s]*)[\'|\"]?[^>]*>#’,$content ,$return);

preg_match_all(‘#]*src\s*=\s*[\'|\"]?((?:http://)?[^\s]+)”[^>]*>#’,$content ,$return);

]]>

« Newer Posts

Powered by 00RZ