简体   繁体   English

使用App Engine使用Python生成随机网址

[英]Generate a random URL with Python using App Engine

I know there have been a number of questions asked and answered on Stackoverflow around this topic but I am hoping that as this question requires some additional information, it warrants its own space or might be useful for others. 我知道关于这个主题在Stackoverflow上有很多问题要问和回答,但是我希望由于这个问题需要一些其他信息,因此可以保证有自己的空间,或者可能对其他人有用。

I have a web app running on app engine using Python and I want to be able to send unique URLs to users and limit the ability for another user to guess a url based off the url they receive. 我有一个使用Python在应用程序引擎上运行的网络应用程序,我希望能够向用户发送唯一的URL,并限制其他用户根据接收到的URL来猜测URL的能力。 I would prefer that users don't need to be authenticated to view the data associated with the url. 我希望不需要对用户进行身份验证即可查看与url相关的数据。

My question is around the best way to create, possibly check and store the urls. 我的问题是关于创建,可能检查和存储URL的最佳方法。 One option could be to use uuid.uuid4() and store this value in the data model to retrieve the information. 一种选择是使用uuid.uuid4()并将此值存储在数据模型中以检索信息。 I could use uuid.uuid5 and use the key_value from the datastore, I could also base64 encode each option or could I possibly simply use the datastore key_value ? 我可以使用uuid.uuid5并使用数据存储区中的key_value ,也可以对每个选项进行base64编码,或者我可以简单地使用数据存储区key_value吗?

Whichever method I choose, will it be unique so I don't need to check if the url already exists in the datastore or is it prudent to do check if it has been generated previously? 无论选择哪种方法,它都将是唯一的,因此我不需要检查URL是否已存在于数据存储中,还是应该谨慎地检查URL是否先前已生成?

The final question is should I store the url in the datastore or use a function to map the url back to existing data in the datastore. 最后一个问题是我应该将网址存储在数据存储区中还是使用函数将网址映射回数据存储区中的现有数据。

So to summarise: 总结一下:

  1. What method do you recommend I use to create the URL and why 您建议我使用哪种方法创建URL,为什么?
  2. Is it prudent to check if the url has been created previously 请谨慎检查网址是否先前已创建
  3. Should I store the URL in the datastore or use a function to map it back 我应该将URL存储在数据存储中还是使用函数将其映射回

Thanks 谢谢

  1. A big enough random string should be good enough, because the probability of collision would be minimal. 足够大的随机字符串应该足够好,因为碰撞的可能性将很小。 I guess you could also use the automatic Key from the Datastore, because they are guaranteed to be unique. 我猜您也可以使用数据存储区中的自动密钥,因为可以保证它们是唯一的。

  2. I don't think that would be necessary if you follow 1. 如果您遵循1,我认为没有必要。

  3. I would make an Entity in the datastore that maps urls to what you want to serve, not through a hash function. 我会在数据存储区中创建一个实体,该实体将url映射到您要提供的服务,而不是通过哈希函数。 That way you will have a better control on what objects you want to make public and what you want to keep private. 这样,您将可以更好地控制要公开的对象和要保密的对象。 And if you see that one url goes public you will be able to change it easily. 而且,如果您看到一个网址公开,则可以轻松对其进行更改。

If what you want to serve is on Cloud Storage you can try Signed URLs 1 . 如果您要在Cloud Storage上提供服务,则可以尝试Signed URLs 1


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM