• 服务器端close_wait模拟


    服务端192.168.137.3上定义接口:
    def testtimedout(req):
       time.sleep(20)
       out={"a":{'ip':'20.20.20.1','db':'aaa'},"c":{'ip':'1.1.1.1','db':'bbb'},"e":{'ip':'2.2.2.2','db':'cccc'}}
       return HttpResponse(json.dumps(out), content_type="application/json")
       
    
    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    import urllib2
    import urllib
    import cookielib
    import json
    import httplib
    import re
    import requests
    import os
    import time
     
    import requests, requests.utils, pickle
     
    try:
        import cookielib  # 兼容Python2
    except:
        import http.cookiejar as cookielib
     
    from requests.packages.urllib3.exceptions import InsecureRequestWarning
     
    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
    url='http://192.168.137.3:9000/testtimedout'
    while True:
       try:
        s = requests.session()
        r = s.get(url, verify=False,timeout=5)
        print r.content
       except:
           print '222222222'
    
    
    
    
    服务器端:
    
    Last login: Mon Aug 26 16:04:31 2019 from 192.168.137.1
    node2:/root#netstat -na |grep 9000
    tcp        0      0 0.0.0.0:9000                0.0.0.0:*                   LISTEN      
    
    
    
    客户端:
    
    C:UsersTLCB>netstat -na | findstr 9000
    
    C:UsersTLCB>
    
    
    
    
    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    import urllib2
    import urllib
    import cookielib
    import json
    import httplib
    import re
    import requests
    import os
    import time
    
    import requests, requests.utils, pickle
    
    try:
        import cookielib  # 兼容Python2
    except:
        import http.cookiejar as cookielib
    
    from requests.packages.urllib3.exceptions import InsecureRequestWarning
    
    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
    url = 'http://192.168.137.3:9000/testtimedout'
    while True:
        try:
            s = requests.session()
            r = s.get(url, verify=False, timeout=5)
            print r.content
            r.close()
        except:
            print '222222222'
    
    
    
    
    服务端close_wait
    node2:/root#netstat -na |grep 9000 | grep -i close
    tcp        1      0 192.168.137.3:9000          192.168.137.1:53583         CLOSE_WAIT  
    tcp        1      0 192.168.137.3:9000          192.168.137.1:53578         CLOSE_WAIT  
    tcp        1      0 192.168.137.3:9000          192.168.137.1:53598         CLOSE_WAIT  
    tcp        1      0 192.168.137.3:9000          192.168.137.1:53571         CLOSE_WAIT  
    tcp        1      0 192.168.137.3:9000          192.168.137.1:53586         CLOSE_WAIT  
    tcp        1      0 192.168.137.3:9000          192.168.137.1:53574         CLOSE_WAIT  
    tcp        1      0 192.168.137.3:9000          192.168.137.1:53588         CLOSE_WAIT  
    tcp        1      0 192.168.137.3:9000          192.168.137.1:53592         CLOSE_WAIT  
    tcp        1      0 192.168.137.3:9000          192.168.137.1:53596         CLOSE_WAIT  
    node2:/root#netstat -na |grep 9000 | grep -i close | wc -l
    
    客户端
    
    C:UsersTLCB>netstat -na | findstr 9000
      TCP    192.168.137.1:54556    192.168.137.3:9000     FIN_WAIT_2
      TCP    192.168.137.1:54558    192.168.137.3:9000     FIN_WAIT_2
      TCP    192.168.137.1:54560    192.168.137.3:9000     FIN_WAIT_2
      TCP    192.168.137.1:54562    192.168.137.3:9000     FIN_WAIT_2
      TCP    192.168.137.1:54564    192.168.137.3:9000     FIN_WAIT_2
      TCP    192.168.137.1:54568    192.168.137.3:9000     FIN_WAIT_2
      TCP    192.168.137.1:54570    192.168.137.3:9000     FIN_WAIT_2
      TCP    192.168.137.1:54574    192.168.137.3:9000     FIN_WAIT_2
      TCP    192.168.137.1:54576    192.168.137.3:9000     FIN_WAIT_2
      TCP    192.168.137.1:54578    192.168.137.3:9000     FIN_WAIT_2
      TCP    192.168.137.1:54580    192.168.137.3:9000     FIN_WAIT_2
      TCP    192.168.137.1:54582    192.168.137.3:9000     FIN_WAIT_2
      TCP    192.168.137.1:54591    192.168.137.3:9000     FIN_WAIT_2
      TCP    192.168.137.1:54593    192.168.137.3:9000     FIN_WAIT_2
      TCP    192.168.137.1:54595    192.168.137.3:9000     FIN_WAIT_2
      TCP    192.168.137.1:54599    192.168.137.3:9000     FIN_WAIT_2
      
    客户端主动关闭联系,服务端堵塞导致
    
    servlet由于得不到数据库连接而阻塞了,这个客户端的请求就一直等待,客户端使用httpclient设置了5s的请求超时时间,
    
    
    那么超时之后就会抛出异常,关闭连接,关闭连接导致客户端发送了FIN报文,
    
    
    我这边的TCP/IP返回了ACK报文,但是由于处理请求的线程还处于阻塞的状态,所以当前的连接状态时CLOSE_WAIT。
    
    
    发现这个状态是由于客户端关闭了socket连接,发送了FIN报文,服务端也发送了ACK报文,此时客户端处于FIN_WAIT_2状态,服务端处于CLOSE_WAIT状态,如下图:
  • 相关阅读:
    hdu 2665 划分树
    概率模型与条件随机场
    shell中各种括号的作用()、(())、[]、[[]]、{}
    今日BBC
    小贝_mysql主从复制作用以及案例
    c++ builder 版CreateAnonymousThread用法
    安卓UI适配限定符
    编译3.10内核 出现错误 “undefined reference to...." 解决方法
    iptables的4表5链(未完)
    已有iptables表的查看
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13348628.html
Copyright © 2020-2023  润新知