Chinaunix首页 | 论坛 | 博客
  • 博客访问: 482111
  • 博文数量: 108
  • 博客积分: 25
  • 博客等级: 民兵
  • 技术积分: 1134
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-29 19:43
文章分类

全部博文(108)

文章存档

2016年(10)

2015年(9)

2014年(73)

2013年(16)

我的朋友

分类: Python/Ruby

2014-11-25 14:08:39


点击(此处)折叠或打开

  1. #!/usr/bin/env python

  2. import socket
  3. import sys
  4. import argparse




  5. def main():
  6.         parser=argparse.ArgumentParser(description='Socket Error Examples')
  7.         parser.add_argument('--host',action='store',dest="host", required=False)
  8.         parser.add_argument('--port',action='store',dest="port", type=int,required=False)
  9.         parser.add_argument('--file',action='store',dest="file", required=False)
  10.         given_args= parser.parse_args()
  11.         host =given_args.host
  12.         port =given_args.port
  13.         filename = given_args.file

  14.         try:
  15.                 s= socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  16.         except socket.error, e:
  17.                 print "Error creating socket : %s" % s
  18.                 sys.exit(1)


  19.         try:
  20.                 s.connect((host,port))
  21.         except socket.gaierror,e:
  22.                 print "Address_related error connecting to server : %s" % e
  23.                 sys.exit(1)

  24.         except socket.error, e:
  25.                 print "Connection error: %s " % e
  26.                 sys.exit(1)

  27.         try:
  28.                 s.sendall("GET %s HTTP/1.0\r\n\r\n " % filename)
  29.         except socket.error, e:
  30.                 print "Error sending data: %s " % e
  31.                 sys.exit(1)

  32.         while 1:
  33. "socket_error.py" 58L, 1206C written
  34. root@MyUbuntu:~# python socket_error.py --host=www.python.org --port=80 --file=1_7_socket_errors.py
  35. HTTP/1.1 500 Domain Not Found
  36. Server: Varnish
  37. Retry-After: 0
  38. content-type: text/html
  39. Cache-Control: private, no-cache
  40. connection: keep-alive
  41. Content-Length: 77
  42. Accept-Ranges: bytes
  43. Date: Tue, 25 Nov 2014 06:06:54 GMT
  44. Via: 1.1 varnish
  45. Connection: close


  46. <html>
  47. <head>
  48. <title> </title>
  49. </head>
  50. <body>
  51. unknown domain: </body></html>root@MyUbuntu:~

阅读(1120) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~