Unicode-objects Array Python 3.7
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unicode-objects Array Python 3.7相关的知识,希望对你有一定的参考价值。
这是我的第二个python项目(我没有经验),我似乎在解决有关编码的问题。我相信数组中存在的问题。我尝试使用类似的分辨率预先编码输入和'297aae72',我发现here .encode('utf-8')
,但我遇到了同样的错误。
我相信这是一个散列问题,因为正在构造的数组中存在编码冲突。
这个文件是speed test.py,我相信找到了错误。
import hashlib
from hashlib import md5
import urllib.request, urllib.error, urllib.parse
import sys
from urllib.parse import parse_qs
import functions
ping = 16
accuracy = 8
server = functions.setup()
def speed_test(up, down):
get_data = [
'download=%s' % down,
'ping=%s' % ping,
'upload=%s' % up,
'promo=',
'startmode=%s' % 'pingselect',
'recommendedserverid=%s' % server,
'accuracy=%s' % 8,
'serverid=%s' % server,
'hash=%s' % md5('%s-%s-%s-%s' %
(ping, up, down, '297aae72')
).hexdigest()]
request = urllib.request.Request('http://www.speedtest.net/api/api.php',
data='&'.join(get_data))
request.add_header('Referer', 'http://c.speedtest.net/flash/speedtest.swf')
connection = urllib.request.urlopen(request)
response = connection.read()
response_code = connection.code
connection.close()
if int(response_code) != 200:
print('There was an issue submitting data')
sys.exit(1)
qs_args = parse_qs(response)
result_id = qs_args.get('resultid')
if not result_id or len(result_id) != 1:
print('No speedtest image found?')
sys.exit(1)
print(('Speedtest Results: http://www.speedtest.net/result/%s.png' % result_id[0]))
down_input = input("Please enter your download speed (EX: 375.520): ")
down_input = down_input.replace(".", "")
up_input = input("Please enter your upload speed (EX: 375.520): ")
up_input = up_input.replace(".", "")
speed_test(up_input, down_input)
这个文件是funcitons.py
import math
import urllib.request, urllib.error, urllib.parse
import os
import time
from xml.dom import minidom as DOM
def calculate_distance(origin, destination):
lat1, lon1 = origin
lat2, lon2 = destination
radius = 6371 # km
latitude = math.radians(lat2 - lat1)
longitude = math.radians(lon2 - lon1)
a = (math.sin(latitude / 2) * math.sin(latitude / 2) + math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) * math.sin(longitude / 2)
* math.sin(longitude / 2))
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
destination = radius * c
return destination
def get_closest_servers(client, complete=False):
connection = urllib.request.urlopen('http://speedtest.net/speedtest-servers.php')
server_xml = connection.read()
if int(connection.code) != 200:
return None
connection.close()
root = DOM.parseString(server_xml)
servers = {}
for server in root.getElementsByTagName('server'):
attrib = dict(list(server.attributes.items()))
d = calculate_distance([float(client['lat']), float(client['lon'])],
[float(attrib.get('lat')), float(attrib.get('lon'))])
attrib['d'] = d
if d not in servers:
servers[d] = [attrib]
else:
servers[d].append(attrib)
closest = []
for d in sorted(servers.keys()):
for s in servers[d]:
closest.append(s)
if len(closest) == 5 and not complete:
break
else:
continue
break
del servers
del root
return closest
def get_best_server(servers):
results = {}
for server in servers:
cum = 0
url = os.path.dirname(server['url'])
for i in range(0, 3):
uh = urllib.request.urlopen('%s/latency.txt' % url)
start = time.time()
text = uh.read().strip()
total = time.time() - start
if int(uh.code) == 200 and text == 'test=test':
cum += total
else:
cum += 3600
uh.close()
avg = round((cum / 3) * 1000000, 3)
results[avg] = server
fastest = sorted(results.keys())[0]
best = results[fastest]
best['latency'] = fastest
return best
def get_config():
uh = urllib.request.urlopen('http://www.speedtest.net/speedtest-config.php')
config_xml = uh.read()
if int(uh.code) != 200:
return None
uh.close()
root = DOM.parseString(config_xml)
config = {
'client': extract_tag_name(root, 'client'),
'times': extract_tag_name(root, 'times'),
'download': extract_tag_name(root, 'download'),
'upload': extract_tag_name(root, 'upload')}
del root
return config
def extract_tag_name(dom, tag_name):
elem = dom.getElementsByTagName(tag_name)[0]
return dict(list(elem.attributes.items()))
def setup():
print('Configuring server, one moment.')
configs = get_config()
closest_server = get_closest_servers(configs['client'])
best = get_best_server(closest_server)
return best['id']
这是我在执行应用程序时收到的错误。
Traceback (most recent call last):
File "speedtest.py", line 54, in <module>
speed_test(up_input, down_input)
File "speedtest.py", line 25, in speed_test
(ping, up, down, '297aae72')
TypeError: Unicode-objects must be encoded before hashing
我在这里错过了什么吗?对变量进行编码后,我的实例中的错误没有变化。
感谢您提供的任何帮助。
答案
这与“数组”没有任何关系;当你定义get_data
时,你正试图做内联的事情,你会感到困惑。实际上,问题是你对md5()
的呼吁;如错误所示,您无法将unicode字符串传递给它,您需要传递字节。所以,只需编码字符串:
hash = md5(('%s-%s-%s-%s' % (ping, up, down, '297aae72')).encode('utf-8')).hexdigest()
以上是关于Unicode-objects Array Python 3.7的主要内容,如果未能解决你的问题,请参考以下文章
django注册在使用hashlib对密码加密时报Unicode-objects must be encoded before hashing
关于python2.7的md5加密遇到的问题(TypeError: Unicode-objects must be encoded before hashing)