-
#!/usr/bin/env python
-
-
import socket
-
import sys
-
import argparse
-
-
-
-
-
def main():
-
parser=argparse.ArgumentParser(description='Socket Error Examples')
-
parser.add_argument('--host',action='store',dest="host", required=False)
-
parser.add_argument('--port',action='store',dest="port", type=int,required=False)
-
parser.add_argument('--file',action='store',dest="file", required=False)
-
given_args= parser.parse_args()
-
host =given_args.host
-
port =given_args.port
-
filename = given_args.file
-
-
try:
-
s= socket.socket(socket.AF_INET,socket.SOCK_STREAM)
-
except socket.error, e:
-
print "Error creating socket : %s" % s
-
sys.exit(1)
-
-
-
try:
-
s.connect((host,port))
-
except socket.gaierror,e:
-
print "Address_related error connecting to server : %s" % e
-
sys.exit(1)
-
-
except socket.error, e:
-
print "Connection error: %s " % e
-
sys.exit(1)
-
-
try:
-
s.sendall("GET %s HTTP/1.0\r\n\r\n " % filename)
-
except socket.error, e:
-
print "Error sending data: %s " % e
-
sys.exit(1)
-
-
while 1:
-
"socket_error.py" 58L, 1206C written
-
root@MyUbuntu:~# python socket_error.py --host=www.python.org --port=80 --file=1_7_socket_errors.py
-
HTTP/1.1 500 Domain Not Found
-
Server: Varnish
-
Retry-After: 0
-
content-type: text/html
-
Cache-Control: private, no-cache
-
connection: keep-alive
-
Content-Length: 77
-
Accept-Ranges: bytes
-
Date: Tue, 25 Nov 2014 06:06:54 GMT
-
Via: 1.1 varnish
-
Connection: close
-
-
-
<html>
-
<head>
-
<title> </title>
-
</head>
-
<body>
-
unknown domain: </body></html>root@MyUbuntu:~
阅读(1120) | 评论(0) | 转发(0) |