1

last time I've gotten some help on making a website name generator. I feel bad but i'm stuck at the moment and I need some help again to improve it. in my code there's a .txt file called combined which included these lines.

Test Output

After that i created a variable to add to the domain

web = 'web'
suffix = 'co.id'

And then i write it out so that the it would print the line output to the Combined.txt

output_count = 50
subdomain_count = 2
for i in range(output_count):
    out = []
    for j in range(subdomain_count):
        out.append(random.choice(Test))
    out.append(web)
    out.append(suffix)
    Example.write('.'.join(out)+"\n")

with open("dictionaries/examples.txt") as f:
    websamples = [line.rstrip() for line in f]

Combined.txt

I want the output where instead of just login.download.web.co.id there would be more variety like login-download.web.co.id or login.download-web.co.id In the code i used Example.write('.'.join(out)+"\n") so that the. would be a separator for each characters. I was thinking of adding more, by making a similar code line and save it to a different .txt files but I feel like it would be too long. Is there a way where I can variate each character separation with this symbol - or _ instead of just a . in the output?

Thanks!

Tsukuru
  • 65
  • 7
  • if you want to replace just a specific occurrence (from your example) you could use https://stackoverflow.com/questions/35091557/replace-nth-occurrence-of-substring-in-string – Andrew Ryan Oct 24 '22 at 07:27
  • Does that even work with an array output @AndrewRyan? Based on the verified answer it seems like it can only replace a string, not all strings. Correct me if i'm wrong. Thx! – Tsukuru Oct 24 '22 at 07:40

3 Answers3

1

Sure just iterate through a list of delimiters to add each of them to the output.

web = 'web'
suffix = 'co.id'
output_count = 50
subdomain_count = 2
delimeters = [ '-', '.']
for i in range(output_count):
    out = []
    for j in range(subdomain_count):
        out.append(random.choice(Test))
    for delimeter in delimeters:        
        addr = delimeter.join(out)
        addrs = '.'.join([addr, web, suffix])
        print(addrs)
        Example.write(addrs + '\n')

output

my_pay.web.co.id
my-pay.web.co.id
my.pay.web.co.id
pay_download.web.co.id
pay-download.web.co.id
pay.download.web.co.id
group_login.web.co.id
group-login.web.co.id
group.login.web.co.id
install_group.web.co.id
install-group.web.co.id
install.group.web.co.id
...
...

update

import itertools

Test = ['download', 'login', 'my', 'ip', 'site', 'ssl', 'pay', 'install']
delimeters = [ '-', '.']

web = 'web'
suffix = 'co.id'
output_count = 50
subdomain_count = 2
for combo in itertools.combinations(Test, 2):
    out = ''
    for i, d in enumerate(delimeters):
        out = d.join(combo)
        out = delimeters[i-1].join([out, web])
        addr = '.'.join([out, suffix])
        print(addr)
        # Example.write(addr+'\n')

output

download-login.web.co.id
download.login-web.co.id
download-my.web.co.id
download.my-web.co.id
download-ip.web.co.id
download.ip-web.co.id
download-site.web.co.id
download.site-web.co.id
download-ssl.web.co.id
download.ssl-web.co.id
download-pay.web.co.id
download.pay-web.co.id
download-install.web.co.id
download.install-web.co.id
login-my.web.co.id
login.my-web.co.id
login-ip.web.co.id
login.ip-web.co.id
login-site.web.co.id
login.site-web.co.id
login-ssl.web.co.id
login.ssl-web.co.id
login-pay.web.co.id
login.pay-web.co.id
login-install.web.co.id
login.install-web.co.id
my-ip.web.co.id
my.ip-web.co.id
my-site.web.co.id
my.site-web.co.id
my-ssl.web.co.id
my.ssl-web.co.id
my-pay.web.co.id
my.pay-web.co.id
my-install.web.co.id
my.install-web.co.id
ip-site.web.co.id
ip.site-web.co.id
ip-ssl.web.co.id
ip.ssl-web.co.id
ip-pay.web.co.id
ip.pay-web.co.id
ip-install.web.co.id
ip.install-web.co.id
site-ssl.web.co.id
site.ssl-web.co.id
site-pay.web.co.id
site.pay-web.co.id
site-install.web.co.id
site.install-web.co.id
ssl-pay.web.co.id
ssl.pay-web.co.id
ssl-install.web.co.id
ssl.install-web.co.id
pay-install.web.co.id
pay.install-web.co.id
Alexander
  • 16,091
  • 5
  • 13
  • 29
  • But is there a way for the TLD (Top Level Domain) or the co.id doesn't join in with the seperators? I want the TLD to not be part of the delimiters. Also, the output is too straight forward and it isn't as vary as i want it to be. – Tsukuru Oct 24 '22 at 07:49
  • @Tsukuru okay now it should work there were a few spelling errors – Alexander Oct 24 '22 at 07:52
  • okay that's quite close, but the web variable it isn't that varied since it kept outputting the `.web` in the output. I'll try to fix the problem but i'm open for it if there's a better way to do it. @Alexander – Tsukuru Oct 24 '22 at 08:15
  • @Tsukuru I thought that is what you wanted... you have the web variable hardcoded in your code so what do you want instead? When and how should the web variable be replaced by something else? – Alexander Oct 24 '22 at 08:16
  • it's quite close, but not exactly what i wanted. the first seperators was quite varied, but the 2nd seperator it kept outputting `.` the only characters that i want `.` to stay is on the third seperators the co.id. @Alexander for example `install-group-web.co.id` or `install.group-web.co.id` or `install.group.web.co.id` – Tsukuru Oct 24 '22 at 08:28
  • I want to know how did you find the library to use for a certain scenario? i'm always confused on which library should i use for example this problem. Sorry if i'm asking a lot, i'm still trying to improve myself on doing python coding @Alexander – Tsukuru Oct 24 '22 at 09:35
  • @Tsukuru I would tell you if I knew the answer. The truth is that it all just comes with experience. Keep practicing and keep asking questions and you will find that you need to ask questions less and less – Alexander Oct 24 '22 at 09:39
  • Thanks for the help @Alexander i'll keep doing my best so i won't be asking more questions next time – Tsukuru Oct 24 '22 at 09:48
  • I want to ask if for example I want to combine the web and test output into a single string for example `downloadweb` can i add another line on top of addr for example `address = ''.join([out, suffix])` and then add it to the `addr ='.'.join([address, out, suffix])` so that there would be a combined string output? – Tsukuru Oct 25 '22 at 03:52
  • @Tsukuru Try adding an empty space `''` to the delimeter list and see if that produces the ouput you are looking for... if not you can try doing what you described but it will produce alot of duplicate output – Alexander Oct 25 '22 at 04:05
  • Sorry @Alexander last question `delimeters[i-1]` for this line, is this used so that there would be not more than 1 delimiters on each character? if so then if I were to add another delimiter would it be become `[i-2]`? – Tsukuru Oct 25 '22 at 04:26
  • @Tsukuru no all that line is doing is grabbing the delimeter in the index just before the current index. You should be able to leave it the way it is. – Alexander Oct 25 '22 at 05:04
  • If you can't get it the way you want it, open a new question with the current code you are using leave the link to the new question in a comment and I will help you figure it out – Alexander Oct 25 '22 at 05:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/249030/discussion-between-tsukuru-and-alexander). – Tsukuru Oct 25 '22 at 07:28
0

As an alternative of replacing the final output, you could make the seperator random:

import random
seperators = ['-', '_', '.']

Example.write(random.choice(seperators).join(out)+"\n")
Alpa
  • 71
  • 1
  • 3
  • This could generate (for example) **login-download.web_co-id** Are you sure that's valid? – DarkKnight Oct 24 '22 at 07:52
  • It's quite close to what i wanted, but i wanted the TLD (Top Level Domain) or the .co.id to stay seperated with the seperators. well I guess I could just seperate the TLD on a different code line so it wouldn't be split up and also i need the `-` , `_` and `.` to be more varied on the output for example user_payment.web.co.id or user-payment.web.co.id @Alpha – Tsukuru Oct 24 '22 at 07:53
  • @Tsukuru You can't use underscore anywhere – DarkKnight Oct 24 '22 at 07:59
  • what does that mean? @OldBill is there no domain that uses underscore anymore or do you mean something different? if it's the early one then i'll erase the underscore on my code – Tsukuru Oct 24 '22 at 08:04
  • 1
    @Tsukuru Domain names that conform to RFC 1035 cannot contain underscore – DarkKnight Oct 24 '22 at 08:06
0

In order to ensure compliance with RFC 1035 I would suggest:

from random import choices as CHOICES, choice as CHOICE

output_count = 50
subdomain_count = 2
web = 'web'
suffix = 'co.id'
dotdash = '.-'

filename = 'output.txt'

Test = [
    'auth',
    'access',
    'account',
    'admin'
    # etc
]

with open(filename, 'w') as output:
    for _ in range(output_count):
        sd = CHOICE(dotdash).join(CHOICES(Test, k=subdomain_count))
        print('.'.join((sd, web, suffix)), file=output)
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
  • From all the answers that i get, I guess it's not possible to make the 2nd separator to have different output variants like for example `my.pay-web.co.id` @OldBill ? – Tsukuru Oct 24 '22 at 09:06
  • 1
    @Tsukuru Anything is possible. You just need to define the requirements properly then implement them – DarkKnight Oct 24 '22 at 09:20
  • thx for the tips! i'll keep trying @OldBill and also thanks for the answer – Tsukuru Oct 24 '22 at 09:43