2
# -*- coding: utf-8 -*-
#!/usr/bin/python3
import socket
# nao tem servidor UDP no google -> vamos usar netcat como servidor UDP!
#Programa de chat: so fala um de cada vez
#implementar falando ao mesmo tempo
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
"""
pacotes_recebidos = client.recvfrom(1024) devolve uma tupla:
(' llallalaaaa\n', ('192.168.1.4', 667))
msg recebida + (IP,porta)
"""
try:
while 6: #while True
#client.sendto(input("Voce: ") + "\n", ("192.168.1.4", 668))
#client.sendto (bytes(input("Voce: ")).encode('utf8') + bytes("\n").encode('utf8'), bytes(("192.168.1.4", 668)).encode('utf8'))
client.sendto((input("Voce: ")).encode('utf8') + ("\n").encode('utf8'),("192.168.1.7", 661))
# endereço do servidor UDP do kali linux usando netcat
msg, friend = client.recvfrom(1024)
print(str(friend) + ": " + str(msg))
#se quiser apenas o ip: use friend[0]
client.close()
except Exception as erro:
print("Conexao falhou ")
print("O erro foi: ", erro)
client.close()
The above program in Python 2.7 worked when changing input
for raw_input
and I don’t know why.
When running in Python 3.5 (with input
instead of raw_input
) i tried to send a "hi" message and the following error occurred:
('The error was: ', Nameerror("name 'hi' is not defined",))
Would you like to make it so that two people can talk simultaneously, how to do it? At the moment, only one person at a time can type the message.
We have to wait for one of the participants to write and type Enter to continue.
This is the client. I am using the netcat
.
Someone could help me?
has a good toturial http://www.bogotobogo.com/python/python_network_programming_tcp_server_client_chat_server_chat_client_select.php
– Miguel
@miguel, thank you
– Ed S
Can you tell me what happens with input() x raw_input() ?
– Ed S
raw_input()
does not exist in python3, it is onlyinput()
, maybe that’s what– Miguel
Running in python 2 with raw_input() works. In python 3 I switch to input() and the error I described appears!
– Ed S
I don’t know, I ran that code here and it didn’t make that mistake
– Miguel
But in python 3.5? I’m using pycharm and python 3.5 in Backbox Linux.
– Ed S
ha... I use python 3.4
– Miguel
@Miguel, I read the tutorial but could not solve. Some suggestion?
– Ed S
See: https://www.youtube.com/watch?v=PkfwX6RjRaI . I did it a few years ago and I remember it’s good.. Python2 but it should make it 3
– Miguel
@Miguel, you said you ran the script in python 3.4 and did not give the error. How did you run it? In pycharm gives error!
– Ed S
It gave but it was not the same that gave to you. Try in line this:
input("Voce: ") + "\n".encode('UTF-8')...
– Miguel
client.sendto( input("Voce: ") +" n".Encode('UTF-8') ("192.168.1.4", 668)) : Syntaxerror: invalid Character in Identifier
– Ed S
I tried: client.sendto (bytes(input("You: "),'ut f-8') + bytes(" n",'ut f-8'), bytes(("192.168.1.4", 668),'utf-8')) . The error was: Unknown encoding: ut f-8
– Ed S
If I shoot utf-8: client.sendto (bytes(input("You: ")) + bytes(" n"), bytes(("192.168.1.4", 668))) ->The error was: string argument without an encoding
– Ed S
I solved the error: only the simultaneous conversation is missing: client.sendto((input("Voce: ")). Encounter('utf8') + (" n"). Encode(' utf 8'),("192.168.1.7", 661)) print(str(Friend) + ": " + str(msg))
– Ed S