http - How to get to know if server closing the connection in python requests module -
i using python requests module in order generate traffic web server,
after request connection suppose break , want run callback if connection got closed( maybe server sending fin or reset flag).
how can find if server or closing connection in requests module, don't want use tcpdump or packet capturing utility verify that.
82 def request_per_session(self, request, method='post', page=none,**options): 83 url=self.url 84 if page:url+=page 85 print pprint.pprint(options) 86 s = requests.session() 87 httpmethodtocall = getattr(s, method) 88 print pprint.pprint(httpmethodtocall) 89 n in range(request): 90 print "running %d" %n 91 if method == 'get': 92 r=httpmethodtocall(url) 93 else: 94 r=httpmethodtocall(url,options) 95 print r.status_code
this can more accomplished port scanner.
to elaborate, i'm assuming sending/receiving data on port 80. information, merely create socket, try connect given host, , if there error can either pass exception or print out. example:
import socket def checkserver(server): s = socket.socket(socket.af_inet, socket.sock_stream) try: socket.connect((socket.gethostbyname(server), 80)) return none except socket.error e: return e
in code, if there no error, nothing passed back(in form of none
), if there error, exception object passed back. code lot cleaner using requests
module because deals low-level connections, great if want check if things open, passing data, , on, terrible sending data, scraping webpages, , on. urllib2
or requests
better suited that.
i hope helps.
Comments
Post a Comment