免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3938 | 回复: 3
打印 上一主题 下一主题

python httplib问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-11-25 15:58 |只看该作者 |倒序浏览
我想写一个IP扫描器。代码如下

#/usr/bin/env python
# -*- coding: utf-8 -*-
import threading,httplib,time,sys
class Mythread(threading.Thread):
        def __init__(self):
                threading.Thread.__init__(self)
        def run(self,host=sys.argv[1]):
                self.host = host
                ports = [80,443,4848,8080,8090,8000,8082,8888,9043,7001,7002,7003,7080,8443,9060,9440,9090,8081,9043,9080,81,18100,9956]
                for port in ports:
                        conn = httplib.HTTPConnection(self.host,port,True,6)
                        i_headers = {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1) Gecko/20090624 Firefox/3.5","Accept": "text/plain"}
                        conn.request('GET','/',headers = i_headers)
                        r1 = conn.getresponse()
                        print r1.status
                        conn.close()
if __name__ == '__main__':
        #global host
        Mythread().start()

但是一运行。

貌似只访问了一个端口。

我的想法是:一次访问每个端口 打印出http状态响应码。

错误如下:


D:\script>python dump.py 220.181.111.147
200
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 551, in __bootstrap_inner
    self.run()
  File "dump.py", line 13, in run
    conn.request('GET','/',headers = i_headers)
  File "C:\Python27\lib\httplib.py", line 958, in request
    self._send_request(method, url, body, headers)
  File "C:\Python27\lib\httplib.py", line 992, in _send_request
    self.endheaders(body)
  File "C:\Python27\lib\httplib.py", line 954, in endheaders
    self._send_output(message_body)
  File "C:\Python27\lib\httplib.py", line 814, in _send_output
    self.send(msg)
  File "C:\Python27\lib\httplib.py", line 776, in send
    self.connect()
  File "C:\Python27\lib\httplib.py", line 757, in connect
    self.timeout, self.source_address)
  File "C:\Python27\lib\socket.py", line 571, in create_connection
    raise err
timeout: timed out

不知道怎么回事了。如果有知道的 还请帮忙解决下 谢谢了

论坛徽章:
0
2 [报告]
发表于 2012-11-25 16:10 |只看该作者

没响应,超时了,试一下用
try:
   conn.request('GET','/',headers = i_headers)

论坛徽章:
1
辰龙
日期:2014-05-22 11:38:58
3 [报告]
发表于 2012-11-25 16:34 |只看该作者
本帖最后由 winway1988 于 2012-11-25 16:58 编辑

加异常处理
  1. #! /bin/env python
  2. # -*- coding: utf-8 -*-

  3. import threading
  4. import httplib
  5. import time
  6. import sys

  7. class Mythread(threading.Thread):
  8.     ports = [80, 443, 4848, 8080, 8090, 8000, 8082, 8888, 9043,
  9.              7001, 7002, 7003, 7080, 8443, 9060, 9440, 9090,
  10.              8081, 9043, 9080, 81, 18100, 9956]
  11.     i_headers = {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1) Gecko/20090624 Firefox/3.5",
  12.                  "Accept": "text/plain"}
  13.     def __init__(self, host):
  14.         threading.Thread.__init__(self)
  15.         self.host = host
  16.     def run(self):
  17.         for port in Mythread.ports:
  18.             try:
  19.                 print 'Connect to %s:%-5s' % (self.host, port),
  20.                 conn = httplib.HTTPConnection(self.host, port, True, 6)
  21.                 conn.request('GET', '/', headers=Mythread.i_headers)
  22.                 r1 = conn.getresponse()
  23.                 print 'Succeed, status: %d' % r1.status
  24.                 conn.close()
  25.             except Exception, e:
  26.                 print 'Failed:', e

  27. if __name__ == '__main__':
  28.     Mythread(sys.argv[1]).start()
复制代码

论坛徽章:
0
4 [报告]
发表于 2012-11-25 19:56 |只看该作者
回复 3# winway1988


    非常感谢 已经解决 谢谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP