服务端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:\Users\TLCB>netstat -na | findstr 9000
C:\Users\TLCB>
# !/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:\Users\TLCB>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状态,如下图: