Cześć, próbuję nawiązać połączenie P2P i otrzymuję błąd 'NoneType' object has no attribute 'sendall' gdy próbuję wysłac wiadomość. połaćzenie jako takie jest nawiązane, gdy wylacze program 1 to w 2 pokazuje sie info o przerwaniu przez hosta.
znalazłem ten kod w pythonie 2.7 i chce go odpalić w 3.6, zmieniłem tylko slowa kluczowe print i input, oraz dodalem encodowanie utf-8, doczytałem ze w 3.6 ma byc byte nie string. w komentarzach ludzie pisali że 2.7 działa, więc najprawdopodobniej to jest problemem.
Czy ktoś potrafi wskazać błąd?
import socket
import threading
import select
import time
def my_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
print(s.getsockname()[0])
s.close()
def main():
class Chat_Server(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.running = 1
self.conn = None
self.addr = None
def run(self):
HOST = ''
PORT = 1776
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(1)
self.conn, self.addr = s.accept()
# Select loop for listen
while self.running == True:
inputready, outputready, exceptready \
= select.select([self.conn], [self.conn], [])
for input_item in inputready:
# Handle sockets
data = self.conn.recv(1024)
if data:
print(str("Them: " + data, 'UTF-8'))
else:
break
time.sleep(0)
def kill(self):
self.running = 0
class Chat_Client(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.host = None
self.sock = None
self.running = 1
def run(self):
PORT = 1776
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.host, PORT))
# Select loop for listen
while self.running == True:
inputready, outputready, exceptready \
= select.select([self.sock], [self.sock], [])
for input_item in inputready:
# Handle sockets
data = self.sock.recv(1024)
if data:
print(str("Them: " + data, 'UTF-8'))
else:
break
time.sleep(0)
def kill(self):
self.running = 0
class Text_Input(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.running = 1
def run(self):
while self.running == True:
text = input()
try:
chat_client.sock.sendall(bytes(text, 'UTF-8'))
except Exception as e:
print(e)
try:
chat_server.conn.sendall(bytes(text, 'UTF-8'))
except Exception as e:
print(e)
time.sleep(0)
def kill(self):
self.running = 0
# Prompt, object instantiation, and threads start here.
ip_addr = input('What IP (or type l to listen)?: ')
if ip_addr == 'l':
chat_server = Chat_Server()
chat_client = Chat_Client()
chat_server.start()
text_input = Text_Input()
text_input.start()
else:
chat_server = Chat_Server()
chat_client = Chat_Client()
chat_client.host = ip_addr
text_input = Text_Input()
chat_client.start()
text_input.start()
if __name__ == "__main__":
main()