找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 4280|回复: 2

[求助] 大神请进,Python 在win环境能运行,在linux下面不能运行。

1

主题

1

帖子

1

积分

贫民

积分
1
vfrisk 发表于 2017-3-16 17:36:40 | 显示全部楼层 |阅读模式
  1. import time
  2. import urllib2
  3. import json

  4. def getJsonData(arg):
  5.     url = "http://ssc.028pay.cn/go.php"
  6.     cmd = "?cmd=" + arg
  7.     request = urllib2.Request(url + cmd)
  8.     response = urllib2.urlopen(request)
  9.     data = response.read()
  10.     return data

  11. def getTime(arg):
  12.     now = time.localtime();
  13.     tmp = time.strftime(arg,now)
  14.     tmp = int(tmp)
  15.     return tmp

  16. def getJsonItem(jsonData,arg):
  17.     json_str = jsonData.decode("UTF-8")
  18.     json_data = json.loads(json_str)
  19.     return json_data[arg]

  20. while True:
  21.     time.sleep(1)
  22.     hour = getTime("%H")
  23.     minute = getTime("%M") % 10
  24.     second = getTime("%S") % 10
  25.     if (hour >= 9 or hour <= 1):
  26.         if (minute >= 0 and minute <= 5):
  27.             if(second == 0):
  28.                 jsonData = getJsonData("check")
  29.                 status = getJsonItem(jsonData, "status")
  30.                 if(status == False):
  31.                     getJsonData("lecai")
  32.                     print("suess")
  33.                 else:
  34.                     print("along")
  35.             else:
  36.                 print("no at second")
  37.         else:
  38.             print("no at minute")
复制代码
上面的上win 下的代码
win 下面的解释器是 2.6.6



  1. #! /usr/bin/python2.6


  2. import time
  3. import urllib2
  4. import json

  5. def getJsonData(arg):
  6.     url = "http://ssc.028pay.cn/go.php"
  7.     cmd = "?cmd=" + arg
  8.     request = urllib2.Request(url + cmd)
  9.     response = urllib2.urlopen(request)
  10.     data = response.read()
  11.     return data


  12. def getTime(arg):
  13.     now = time.localtime();
  14.     tmp = time.strftime(arg,now)
  15.     tmp = int(tmp)
  16.     return tmp

  17. def getJsonItem(jsonData,arg):
  18.     json_str = jsonData.decode("UTF-8")
  19.     json_data = json.loads(json_str)
  20.     return json_data[arg]



  21. while True:
  22.     time.sleep(1)
  23.     hour = getTime("%H")
  24.     minute = getTime("%M") % 10
  25.     second = getTime("%S") % 10
  26.     if (hour >= 9 or hour <= 1):
  27.         if (minute >= 0 and minute <= 5):
  28.             if(second == 0):
  29.                 jsonData = getJsonData("check")
  30.                 status = getJsonItem(jsonData, "status")
  31.                 if(status == False):
  32.                     getJsonData("lecai")
复制代码
这个linux的代码
linux下的解释器也是 2.6

这是错误提示,为毛呀
  1. Traceback (most recent call last):
  2.   File "/www/wdlinux/python/task.py", line 34, in <module>
  3.     jsonData = getJsonData("check")
  4.   File "/www/wdlinux/python/task.py", line 11, in getJsonData
  5.     response = urllib2.urlopen(request)
  6.   File "/usr/lib64/python2.6/urllib2.py", line 126, in urlopen
  7.     return _opener.open(url, data, timeout)
  8.   File "/usr/lib64/python2.6/urllib2.py", line 397, in open
  9.     response = meth(req, response)
  10.   File "/usr/lib64/python2.6/urllib2.py", line 510, in http_response
  11.     'http', request, response, code, msg, hdrs)
  12.   File "/usr/lib64/python2.6/urllib2.py", line 435, in error
  13.     return self._call_chain(*args)
  14.   File "/usr/lib64/python2.6/urllib2.py", line 369, in _call_chain
  15.     result = func(*args)
  16.   File "/usr/lib64/python2.6/urllib2.py", line 518, in http_error_default
  17.     raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
  18. urllib2.HTTPError: HTTP Error 403: Forbidden
复制代码



回复

使用道具 举报

4

主题

24

帖子

24

积分

贫民

积分
24
pythonCXZ 发表于 2017-3-17 08:57:08 | 显示全部楼层
说的很清楚了吧
urllib2.HTTPError: HTTP Error 403: Forbidden

代码应该没毛病,只是访问的网址报404 ,check下你的传入的URL
回复 支持 反对

使用道具 举报

3

主题

278

帖子

278

积分

侠客

积分
278

热心会员

uitb 发表于 2017-3-17 13:23:00 | 显示全部楼层
urllib2.HTTPError: HTTP Error 403: Forbidden
应该是被网站判断为非浏览器、禁止访问了吧,加个伪装头部就行
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表