chi*_*l0r 1 python debian urllib2 python-3.x
我在debian服务器上运行的一个小python脚本中遇到了一些问题.
首先它应该做什么:
- 从服务器获取列表 - >工作
- 转换为真正的字符串列表 - >工作
- 写入文件 - >什么都不做...
已经尝试在python接口(>>>)中使用相同的代码,它按照应该的方式编写所有内容.
文件已经创建并且上面有一个chmod 777.
即使没有意外检查该scipt的另一个实例正在运行哪个锁定文件但没有...
任何人都知道为什么它不会在启动时写入文件但在界面中?
现在这里是脚本本身:
#!/usr/bin/env python
import urllib
import sys
import time
import re
exitNodes = []
readableNodes = []
class BlockTor():
def getListFromWeb(myself):
url = "https://www.dan.me.uk/torlist/"
#url = "file:///E:/test.txt"
try:
for line in urllib.request.urlopen(url):
exitNodes.append(str(line, encoding='utf8'))
for node in exitNodes:
readableNodes.append(re.sub('\\n', '', node))
#print(exitNodes)
#sys.exit()
except:
try:
f = open("/var/log/torblocker.log", "a")
#f = open("E:\\logfile.log", "a")
f.write("[" + time.strftime("%a, %d %b %Y %H:%M") + "] Error while loading new tornodes")
f.close()
except:
print("Can't write log")
pass
sys.exit()
pass
def buildList(myself):
f = open("/etc/apache2/torlist/tor-ip.conf", "w")
#f = open ("E:\\test-ips.conf", "w")
f.write('<RequireAll>\n')
f.write(' Require all granted\n')
for line in readableNodes:
f.write(' Require not ip ' + line + '\n')
f.write('</RequireAll>')
f.close()
try:
f = open("/var/log/torblocker.log", "a")
#f = open("E:\\logfile.log", "a")
f.write("[" + time.strftime("%a, %d %b %Y %H:%M") + "] Sucessfully updated tor blacklist")
f.close()
except:
pass
def torhandler(myself):
BlockTor.getListFromWeb(myself)
BlockTor.buildList(myself)
if __name__ == "__main__":
asdf = BlockTor()
BlockTor.torhandler(asdf)
Run Code Online (Sandbox Code Playgroud)
编辑:
忘了提 - 如果你想测试它要小心:这台服务器每30分钟只允许一个请求
要连接字符串,请使用+运算符.&是按位AND - 用两个字符串调用它将导致TypeError:
>>> "[" & ""
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for &: 'str' and 'str'
Run Code Online (Sandbox Code Playgroud)
你的毯子可以except防止这种错误.更换
try:
f = open("/var/log/torblocker.log", "a")
#f = open("E:\\logfile.log", "a")
f.write("[" & time.strftime("%a, %d %b %Y %H:%M") & "] Sucessfully updated tor blacklist")
f.close() # ^ ^
except:
pass
Run Code Online (Sandbox Code Playgroud)
同
with open("/var/log/torblocker.log", "a") as torf:
torf.write("[" + time.strftime("%a, %d %b %Y %H:%M") + "] " +
"Sucessfully updated tor blacklist")
Run Code Online (Sandbox Code Playgroud)
如果要在无法写入文件时忽略异常,请将其包围try .. except IOError:.
| 归档时间: |
|
| 查看次数: |
13771 次 |
| 最近记录: |