• B правой части каждого сообщения есть стрелки и . Не стесняйтесь оценивать ответы. Чтобы автору вопроса закрыть свой тикет, надо выбрать лучший ответ. Просто нажмите значок в правой части сообщения.

  • Курсы Академии Кодебай, стартующие в мае - июне, от команды The Codeby

    1. Цифровая криминалистика и реагирование на инциденты
    2. ОС Linux (DFIR) Старт: 16 мая
    3. Анализ фишинговых атак Старт: 16 мая Устройства для тестирования на проникновение Старт: 16 мая

    Скидки до 10%

    Полный список ближайших курсов ...

Чат на python.Как создать логин?

Rock

Member
26.06.2021
14
0
BIT
0
У меня есть простой чат на Python.Но, такое дело то что нужно ввести IP адрес того,кому хочешь написать.Можно ли сделать так, что-бы можно было создать логин,который будет носить значение IP адреса......

клиент
Python:
#КЛИЕНТ


import time, socket, sys
import colorama

socket_server = socket.socket()
server_host = socket.gethostname()
ip = socket.gethostbyname(server_host)
sport = 8080

print('This is your IP address: ',ip)
server_host = input('Enter friend\'s IP address:')
name = input('Enter Friend\'s name: ')




socket_server.connect((server_host, sport))

socket_server.send(name.encode())
server_name = socket_server.recv(1024)
server_name = server_name.decode()

print(server_name,' has joined...')
while True:
    message = (socket_server.recv(1024)).decode()
    print(server_name, ":", message)
    

    message = input(Fore.RED + "Me : ")
    print(Style.RESET_ALL)
    socket_server.send(message.encode())


сервер

Python:
#СЕРВЕР


import time, socket, sys
from colorama import Fore, Back, Style

new_socket = socket.socket()
host_name = socket.gethostname()
s_ip = socket.gethostbyname(host_name)

port = 8080

new_socket.bind((host_name, port))
print( "Binding successful!")
print('This is your IP: ', s_ip)
#print('your Internet may be under threat of ddos attacks, come up with a login')
#login = input('Login: ')






name = input('Enter name: ')

new_socket.listen(1)


conn, add = new_socket.accept()

print("Received connection from ", add[0])
print('Connection Established. Connected From: ',add[0])

client = (conn.recv(1024)).decode()
print(client + ' has connected.')

conn.send(name.encode())
while True:
 
    message = input('Me : ')
    conn.send(message.encode())
    message = conn.recv(1024)
    message = message.decode()
    print(client, ':', message)
 

MrHacker1337_228

One Level
02.05.2020
50
0
BIT
159
типо так?
Python:
#КЛИЕНТ


import time, socket, sys
import colorama
import json
import re

def ackIpInput():
    while True:
        server_host = input('Enter friend\'s IP address:')
        match = re.match(r'(\d{1,3}\.){3}\d{1,3}',server_host)
        if match:
            if server_host != match.string:
                print('Incorrect ip address')
                continue
            else:
                addNicknameRecord(name, server_host)
                return

def addNicknameRecord(nickname, ip):
    file = open('log.txt','r+')
    try:
        data = json.loads(file.read())
    except json.decoder.JSONDecodeError:
        data = {}
    data[nickname] = ip
    file.seek(0)
    file.write(json.dumps(data))
    file.close()
    print('record added/changed')
    return 1

def getNicknameRecordExist(nickname):
    file = open('log.txt','r+')
    try:
        data = json.loads(file.read())
    except json.decoder.JSONDecodeError:
        data = {}
    file.close()
    for key in data:
        if nickname == key:
            return nickname
    return False


    


socket_server = socket.socket()
server_host = socket.gethostname()
ip = socket.gethostbyname(server_host)
sport = 8080

print('This is your IP address: ',ip)

name = input('Enter Friend\'s name: ')
raddr = getNicknameRecordExist(name)
if raddr:
    print(f'saved ip address of this nickname are:{raddr}')
    q = input('do you want to change it? y\\n ')
    if q.lower() == 'y':
        ackIpInput()
else:
    ackIpInput()






socket_server.connect((server_host, sport))

socket_server.send(name.encode())
server_name = socket_server.recv(1024)
server_name = server_name.decode()

print(server_name,' has joined...')
while True:
    message = (socket_server.recv(1024)).decode()
    print(server_name, ":", message)
    

    message = input(Fore.RED + "Me : ")
    print(Style.RESET_ALL)
    socket_server.send(message.encode())
 

Rock

Member
26.06.2021
14
0
BIT
0
типо так?
Python:
#КЛИЕНТ


import time, socket, sys
import colorama
import json
import re

def ackIpInput():
    while True:
        server_host = input('Enter friend\'s IP address:')
        match = re.match(r'(\d{1,3}\.){3}\d{1,3}',server_host)
        if match:
            if server_host != match.string:
                print('Incorrect ip address')
                continue
            else:
                addNicknameRecord(name, server_host)
                return

def addNicknameRecord(nickname, ip):
    file = open('log.txt','r+')
    try:
        data = json.loads(file.read())
    except json.decoder.JSONDecodeError:
        data = {}
    data[nickname] = ip
    file.seek(0)
    file.write(json.dumps(data))
    file.close()
    print('record added/changed')
    return 1

def getNicknameRecordExist(nickname):
    file = open('log.txt','r+')
    try:
        data = json.loads(file.read())
    except json.decoder.JSONDecodeError:
        data = {}
    file.close()
    for key in data:
        if nickname == key:
            return nickname
    return False


   


socket_server = socket.socket()
server_host = socket.gethostname()
ip = socket.gethostbyname(server_host)
sport = 8080

print('This is your IP address: ',ip)

name = input('Enter Friend\'s name: ')
raddr = getNicknameRecordExist(name)
if raddr:
    print(f'saved ip address of this nickname are:{raddr}')
    q = input('do you want to change it? y\\n ')
    if q.lower() == 'y':
        ackIpInput()
else:
    ackIpInput()






socket_server.connect((server_host, sport))

socket_server.send(name.encode())
server_name = socket_server.recv(1024)
server_name = server_name.decode()

print(server_name,' has joined...')
while True:
    message = (socket_server.recv(1024)).decode()
    print(server_name, ":", message)
   

    message = input(Fore.RED + "Me : ")
    print(Style.RESET_ALL)
    socket_server.send(message.encode())
после ввода имени другого устройства вылетает (все модули установлены , вопрос вызывает только 're' )
 

MrHacker1337_228

One Level
02.05.2020
50
0
BIT
159
извиняюсь, пожалуй, должно быть так
Python:
import time, socket, sys
import colorama
import json
import re

def ackIpInput():
    while True:
        server_host = input('Enter friend\'s IP address:')
        match = re.match(r'(\d{1,3}\.){3}\d{1,3}',server_host)
        if match:
            if server_host != match.string:
                print('Incorrect ip address')
                continue
            else:
                addNicknameRecord(name, server_host)
                return server_host

def addNicknameRecord(nickname, ip):
    file = open('log.txt','r+')
    try:
        data = json.loads(file.read())
    except json.decoder.JSONDecodeError:
        data = {}
    data[nickname] = ip
    file.seek(0)
    file.write(json.dumps(data))
    file.close()
    print('record added/changed')
    return 1

def getNicknameRecordExist(nickname):
    file = open('log.txt','r+')
    try:
        data = json.loads(file.read())
    except json.decoder.JSONDecodeError:
        data = {}
    file.close()
    for key in data:
        if nickname == key:
            return data[nickname]
    return False


  


socket_server = socket.socket()
server_host = socket.gethostname()
ip = socket.gethostbyname(server_host)
sport = 8080

print('This is your IP address: ',ip)

name = input('Enter Friend\'s name: ')
raddr = getNicknameRecordExist(name)
if raddr:
    print(f'saved ip address of this nickname are:{raddr}')
    q = input('do you want to change it? y\\n ')
    if q.lower() == 'y':
        server_host = ackIpInput()
    else:
        server_host = raddr
        print(raddr)
else:
    ackIpInput()






socket_server.connect((server_host, sport))

socket_server.send(name.encode())
server_name = socket_server.recv(1024)
server_name = server_name.decode()

print(server_name,' has joined...')
while True:
    message = (socket_server.recv(1024)).decode()
    print(server_name, ":", message)
  

    message = input(Fore.RED + "Me : ")
    print(Style.RESET_ALL)
    socket_server.send(message.encode())
 

Rock

Member
26.06.2021
14
0
BIT
0
извиняюсь, пожалуй, должно быть так
Python:
import time, socket, sys
import colorama
import json
import re

def ackIpInput():
    while True:
        server_host = input('Enter friend\'s IP address:')
        match = re.match(r'(\d{1,3}\.){3}\d{1,3}',server_host)
        if match:
            if server_host != match.string:
                print('Incorrect ip address')
                continue
            else:
                addNicknameRecord(name, server_host)
                return server_host

def addNicknameRecord(nickname, ip):
    file = open('log.txt','r+')
    try:
        data = json.loads(file.read())
    except json.decoder.JSONDecodeError:
        data = {}
    data[nickname] = ip
    file.seek(0)
    file.write(json.dumps(data))
    file.close()
    print('record added/changed')
    return 1

def getNicknameRecordExist(nickname):
    file = open('log.txt','r+')
    try:
        data = json.loads(file.read())
    except json.decoder.JSONDecodeError:
        data = {}
    file.close()
    for key in data:
        if nickname == key:
            return data[nickname]
    return False


 


socket_server = socket.socket()
server_host = socket.gethostname()
ip = socket.gethostbyname(server_host)
sport = 8080

print('This is your IP address: ',ip)

name = input('Enter Friend\'s name: ')
raddr = getNicknameRecordExist(name)
if raddr:
    print(f'saved ip address of this nickname are:{raddr}')
    q = input('do you want to change it? y\\n ')
    if q.lower() == 'y':
        server_host = ackIpInput()
    else:
        server_host = raddr
        print(raddr)
else:
    ackIpInput()






socket_server.connect((server_host, sport))

socket_server.send(name.encode())
server_name = socket_server.recv(1024)
server_name = server_name.decode()

print(server_name,' has joined...')
while True:
    message = (socket_server.recv(1024)).decode()
    print(server_name, ":", message)
 

    message = input(Fore.RED + "Me : ")
    print(Style.RESET_ALL)
    socket_server.send(message.encode())
import time, socket, sys
import colorama
import json
import re
def ackIpInput():
while True:
server_host = input('Enter friend\'s IP address:')
match = re.match(r'(\d{1,3}\.){3}\d{1,3}',server_host)
if match:
if server_host != match.string:
print('Incorrect ip address')
continue
else:
addNicknameRecord(name, server_host)
return server_host
def addNicknameRecord(nickname, ip):
file = open('log.txt','r+')
try:
data = json.loads(file.read())
except json.decoder.JSONDecodeError:
data = {}
data[nickname] = ip
file.seek(0)
file.write(json.dumps(data))
file.close()
print('record added/changed')
return 1
def getNicknameRecordExist(nickname):
file = open('log.txt','r+')
try:
data = json.loads(file.read())
except json.decoder.JSONDecodeError:
data = {}
file.close()
for key in data:
if nickname == key:
return data[nickname]
return False



socket_server = socket.socket()
server_host = socket.gethostname()
ip = socket.gethostbyname(server_host)
sport = 8080
print('This is your IP address: ',ip)
name = input('Enter Friend\'s name: ')
raddr = getNicknameRecordExist(name)
if raddr:
print(f'saved ip address of this nickname are:{raddr}')
q = input('do you want to change it? y\\n ')
if q.lower() == 'y':
server_host = ackIpInput()
else:
server_host = raddr
print(raddr)
else:
ackIpInput()





socket_server.connect((server_host, sport))
socket_server.send(name.encode())
server_name = socket_server.recv(1024)
server_name = server_name.decode()
print(server_name,' has joined...')
while True:
message = (socket_server.recv(1024)).decode()
print(server_name, ":", message)

message = input(Fore.RED + "Me : ")
print(Style.RESET_ALL)
socket_server.send(message.encode())
 

Rock

Member
26.06.2021
14
0
BIT
0
Это стандартная библиотека

Вылетает с какой ошибкой?
Ну вот как то так

import time, socket, sys
import colorama
import json
import re
def ackIpInput():
while True:
server_host = input('Enter friend\'s IP address:')
match = re.match(r'(\d{1,3}\.){3}\d{1,3}',server_host)
if match:
if server_host != match.string:
print('Incorrect ip address')
continue
else:
addNicknameRecord(name, server_host)
return server_host
def addNicknameRecord(nickname, ip):
file = open('log.txt','r+')
try:
data = json.loads(file.read())
except json.decoder.JSONDecodeError:
data = {}
data[nickname] = ip
file.seek(0)
file.write(json.dumps(data))
file.close()
print('record added/changed')
return 1
def getNicknameRecordExist(nickname):
file = open('log.txt','r+')
try:
data = json.loads(file.read())
except json.decoder.JSONDecodeError:
data = {}
file.close()
for key in data:
if nickname == key:
return data[nickname]
return False



socket_server = socket.socket()
server_host = socket.gethostname()
ip = socket.gethostbyname(server_host)
sport = 8080
print('This is your IP address: ',ip)
name = input('Enter Friend\'s name: ')
raddr = getNicknameRecordExist(name)
if raddr:
print(f'saved ip address of this nickname are:{raddr}')
q = input('do you want to change it? y\\n ')
if q.lower() == 'y':
server_host = ackIpInput()
else:
server_host = raddr
print(raddr)
else:
ackIpInput()





socket_server.connect((server_host, sport))
socket_server.send(name.encode())
server_name = socket_server.recv(1024)
server_name = server_name.decode()
print(server_name,' has joined...')
while True:
message = (socket_server.recv(1024)).decode()
print(server_name, ":", message)

message = input(Fore.RED + "Me : ")
print(Style.RESET_ALL)
socket_server.send(message.encode())
 

Rock

Member
26.06.2021
14
0
BIT
0
извиняюсь, пожалуй, должно быть так
Python:
import time, socket, sys
import colorama
import json
import re

def ackIpInput():
    while True:
        server_host = input('Enter friend\'s IP address:')
        match = re.match(r'(\d{1,3}\.){3}\d{1,3}',server_host)
        if match:
            if server_host != match.string:
                print('Incorrect ip address')
                continue
            else:
                addNicknameRecord(name, server_host)
                return server_host

def addNicknameRecord(nickname, ip):
    file = open('log.txt','r+')
    try:
        data = json.loads(file.read())
    except json.decoder.JSONDecodeError:
        data = {}
    data[nickname] = ip
    file.seek(0)
    file.write(json.dumps(data))
    file.close()
    print('record added/changed')
    return 1

def getNicknameRecordExist(nickname):
    file = open('log.txt','r+')
    try:
        data = json.loads(file.read())
    except json.decoder.JSONDecodeError:
        data = {}
    file.close()
    for key in data:
        if nickname == key:
            return data[nickname]
    return False


 


socket_server = socket.socket()
server_host = socket.gethostname()
ip = socket.gethostbyname(server_host)
sport = 8080

print('This is your IP address: ',ip)

name = input('Enter Friend\'s name: ')
raddr = getNicknameRecordExist(name)
if raddr:
    print(f'saved ip address of this nickname are:{raddr}')
    q = input('do you want to change it? y\\n ')
    if q.lower() == 'y':
        server_host = ackIpInput()
    else:
        server_host = raddr
        print(raddr)
else:
    ackIpInput()






socket_server.connect((server_host, sport))

socket_server.send(name.encode())
server_name = socket_server.recv(1024)
server_name = server_name.decode()

print(server_name,' has joined...')
while True:
    message = (socket_server.recv(1024)).decode()
    print(server_name, ":", message)
 

    message = input(Fore.RED + "Me : ")
    print(Style.RESET_ALL)
    socket_server.send(message.encode())

Не работает.Я перепроверил все модули.Я честно говоря не знаю почему?
 
Мы в соцсетях:

Обучение наступательной кибербезопасности в игровой форме. Начать игру!