Python网络编程测试-socket初探
Python网络编程测试-socket初探
由于项目需要很多数据,虽然网上有些研究中心发布的数据库,但是都要收费,对于我这种三无人员来说简直是奢侈。
(一个人脸识别的数据库要1000刀!WTF!)
慢慢试着自己开发一个搜索工具吧。。。
先测试一个socket小小功能函数吧(非常小,高手就路过吧。。。)
[python] #testint of sending infomation to www.2cto.com
import sys , socket , time
host = sys.argv[1]
xport = sys.argv[2]
filename = sys.argv[3]
print('getting socket...')
try:
s = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
except socket.error , e:
print('here is a error: %s') % e
sys.exit(1)
print('done')
print('getting socket port')
try:
port = int(xport)
except ValueError:
try:
port = socket.getprotobyname(xport , 'tcp')
except socket.error , e:
print('here is an error happened: %s') % e
sys.exit(1)
print('done')
print('connecting the host')
try:
s.connect((host , port))
except socket.gaierror , e:
print('here is an error happened: %s') % e
sys.exit(1)
except socket.error , e:
print('here is an error happened: %s') % e
sys.exit(1)
print('done')
print('sleeping...')
time.sleep(3)
print('connecting...')
try:
s.sendall('GET %s HTTP/1.0\r\n\r\n' % filename)
except socket.error , e:
print('here is an error happened: %s') % e
sys.exit(1)
try:
s.shutdown(1)
except socket.error , e:
print('here is an error happened: %s') % e
sys.exit(1)
while True:
try:
buf = s.recv(64)
except socket.error , e:
print('here is an error happened: %s') % e
sys.exit(1)
if not len(buf):
break
sys.stdout.write(buf)
#testint of sending infomation to www.2cto.com
import sys , socket , time
host = sys.argv[1]
xport = sys.argv[2]
filename = sys.argv[3]
print('getting socket...')
try:
s = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
except socket.error , e:
print('here is a error: %s') % e
sys.exit(1)
print('done')
print('getting socket port')
try:
port = int(xport)
except ValueError:
try:
port = socket.getprotobyname(xport , 'tcp')
except socket.error , e:
print('here is an error happened: %s') % e
sys.exit(1)
print('done')
print('connecting the host')
try:
s.connect((host , port))
except socket.gaierror , e:
print('here is an error happened: %s') % e
sys.exit(1)
except socket.error , e:
print('here is an error happened: %s') % e
sys.exit(1)
print('done')
print('sleeping...')
time.sleep(3)
print('connecting...')
try:
s.sendall('GET %s HTTP/1.0\r\n\r\n' % filename)
except socket.error , e:
print('here is an error happened: %s') % e
sys.exit(1)
try:
s.shutdown(1)
except socket.error , e:
print('here is an error happened: %s') % e
sys.exit(1)
while True:
try:
buf = s.recv(64)
except socket.error , e:
print('here is an error happened: %s') % e
sys.exit(1)
if not len(buf):
break
sys.stdout.write(buf)
Python的socket模块(2.6版本,3.2的还没试,估计差不多)定义了4种可能出现的错误:
1 与一般IO和通信问题相关:socket.error
2 与查询地址信息相关:socket.gaierror
3 与其他地址错误相关:socket.herror
4 与在一个socket上调用settimeout()后,处理超时相关: socket.timeout
上面的code有个重大问题,那就是没有将socket关闭,因为要测试socket模块shutdown函数(因为如果程序设计不当,shutdown函数会引发一些问题),最后加上这个代码段就可以了
[python] try:
s.shutdown(1)
except socket.error , e:
print('here is an error: %s') % e
sys.exit(1)
try:
s.shutdown(1)
except socket.error , e:
print('here is an error: %s') % e
sys.exit(1)
lol
摘自 FishinLab的专栏
相关内容
- 暂无相关文章
评论关闭