python实现带错误处理功能的远程文件读取方法

yipeiwu_com6年前Python基础

本文实例讲述了python实现带错误处理功能的远程文件读取方法。分享给大家供大家参考。具体如下:

import socket, sys, time
host = sys.argv[1]
textport = "80"
filename = sys.argv[3]
try:
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  port = int(textport)
  s.connect((host, port))
  fd = s.makefile('rw', 0)
  print "sleeping..."
  time.sleep(10)
  print "Continuing."
  fd.write("GET %s HTTP/1.0\r\n\r\n" % filename)
  fd.flush()    
  s.shutdown(1)  
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)
while 1:
  try:
    buf = fd.read(2048)
  except socket.error, e:
    print "Error receiving data: %s" % e
    sys.exit(1)
  if not len(buf):
    break
  sys.stdout.write(buf)

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Python批量查询关键词微信指数实例方法

Python批量查询关键词微信指数实例方法

教你用Python批量查询关键词微信指数。 前期准备安装好Python开发环境及Fiddler抓包工具。前期准备安装好Python开发环境及Fiddler抓包工具。 首先打开Fiddle...

python判断一个对象是否可迭代的例子

如何判断一个对象是可迭代对象? 方法是通过collections模块的Iterable类型判断: >>> from collections import Iter...

Python脚本实现网卡流量监控

#/usr/bin/env/python #coding=utf-8 import sys,re,time,os maxdata = 50000 #单位KB memfilename...

详解python开发环境搭建

详解python开发环境搭建

虽然网上有很多python开发环境搭建的文章,不过重复造轮子还是要的,记录一下过程,方便自己以后配置,也方便正在学习中的同事配置他们的环境。 1.准备好安装包 1)上python官网下载...

windows下python连接oracle数据库

python连接oracle数据库的方法,具体如下 1.首先安装cx_Oracle包 2.解压instantclient-basic-windows.x64-11.2.0.4.0.zip...