使用python生成促销代码
通过使用 python 语言,这将是一种聪明/有效的生成促销代码的方法。 喜欢用于生成折扣优惠券的特殊号码。 喜欢:1027828-1
谢谢
By using python language, what would be a clever / efficient way of generating promotion codes.
Like to be used for generating special numbers for discount coupons.
like: 1027828-1
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以下内容并不是特别Pythonic或特别高效,但可能就足够了:
The following isn't particularly pythonic or particularly efficient, but it might suffice:
1027828-1 非常小。攻击者仅使用几行代码甚至几天的时间就可以做出约百万次猜测。
这是使用 python 生成难以预测的数字的好方法,它可以在 Linux 和 Windows 下运行。它是为了二进制安全而进行 base64 编码的,具体取决于您使用它做什么,您可能需要 urllib.urlencode() 但我会避免使用 base10,因为它不会存储那么多信息。
附带说明一下,这会生成一个完整的字节,即 base256。 256^8 是 18446744073709551616 应该足够大。
正如指出的那样,base64 对于人类来说并不是一个很好的标记。考虑使用替代编码,例如 url-safe base64 或 humanhash 因为它们更容易输入。
1027828-1 is extremely small. An attacker can make a ~million guesses using only a couple lines of code and maybe a few days.
This is a good way to produce a hard to predict number using python, it works under linux and windows. It is base64'ed for binary safety, depending what you are doing with it you might want to urllib.urlencode() but I would avoid base10 because it doesn't store as much information.
As a side note this is generating a full byte, which is base256. 256^8 is 18446744073709551616 which should be large enough.
As it was pointed out base64 isn't a very good token for humans to use. Consider an alternate encoding like url-safe base64 or perhaps humanhash as they would be easier to type in.
如果您需要 6 位数字 #,您可以这样做,直到找到唯一值:
或按顺序...
if you need a 6-digit # you could do this until you found a unique value:
or go sequentially...
试试这个:
只要你愿意,你就可以制作优惠券。他们还保存到名为 coupons.txt 的 txt 文件中。
Try this:
You can make the coupons as long as you want. They save into a txt file called coupons.txt also.
我对此提出了一个我认为相当聪明的答案,但依赖于一些可能不适合您的情况的假设。
如果这些对您有用,那么这个解决方案也可能对您有用:
在此函数中,使用字符串类型的种子作为代码的基础。对于字符串中的每个字符,将其转换为其 ASCII 表示形式,然后将其附加到代码中。
默认情况下,该函数生成如下代码:
'97534957569756524557'
,并且可以使用任意种子调用。例如...I've come up with an answer for this that I think is fairly clever, but relies on a couple of assumptions that might not be true for your situation.
If these work for you, then so might this solution:
In this function, a string-typed seed is used as the base of the code. For each character in the string, convert it into its ASCII representation, then append it to the code.
By default, the function yields codes like this:
'97534957569756524557'
, and can be invoked with any arbitrary seed. For example...