所以,这是代码:
#!/usr/bin/python
from sys import exit
import urllib.request
answer = urllib.request.urlopen("http://monip.org").read()
def debug(txt):
print(txt)
exit(0)
def parse_answer(answer):
''' Simple function to parse request's HTML result
to find the ip in it. Raise RuntimeError if no
ip in result and ip else.
'''
import re
pattern = "^\w+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\w+$"
regexp = re.compile(pattern)
if regexp.match(regexp, answer):
m = regexp.search(regexp, answer)
ip = m.group(0)
return ip
else:
raise RuntimeError
try:
ip = parse_answer(answer)
except RuntimeError:
print("Error, check your network configuration.")
print("Aborting..")
exit(1)
print("IP:", ip)
这是我写的。此代码旨在为您提供您的公共(public) IP 地址。如果它不能给你任何东西,它会抛出一个运行时错误。
这是错误:
Traceback (most recent call last): File "./ippub", line 27, in ip = parse_answer(answer) File "./ippub", line 19, in parse_answer if regexp.match(regexp, answer): TypeError: 'bytes' object cannot be interpreted as an integer
这意味着“answer”变量是字节,但我想匹配其中的 IP 地址,但由于 python 类型系统,我不能:-)
有什么想法吗?非常感谢!
最佳答案
您有两个不同的问题。
您需要转换
answer
到一个字符串,即使answer
有一些有趣的字符不能用utf-8
很好地解码.您错误地调用了正则表达式 API。
这是一个更正的版本,它使用 chr
解决问题 1,并使用正确的语法修复问题 2。
#!/usr/bin/python
from sys import exit
import urllib.request
import re
def debug(txt):
print(txt)
exit(0)
def parse_answer(answer):
''' Simple function to parse request's HTML result
to find the ip in it. Raise RuntimeError if no
ip in result and ip else.
'''
answer = "".join([chr(x) for x in answer])
pattern = "(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
regexp = re.compile(pattern)
m = regexp.search(answer)
if m:
ip = m.group(0)
return ip
else:
raise RuntimeError
answer = urllib.request.urlopen("http://monip.org").read()
try:
ip = parse_answer(answer)
except RuntimeError:
print("Error, check your network configuration.")
print("Aborting..")
exit(1)
print("IP:", ip)
关于regex - 正则表达式中的字节错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25073131/