Python networking programming through sockets - python

I don't know how to make the server to take the command from the user ... I have untill now this code for the server
import socket, sys
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#HOST = sys.argv.pop() if len(sys.argv) == 3 else '127.0.0.1'
HOST = 'localhost'
PORT = 8080
def recv_all(sock, length):
data = ''
while len(data) < length:
more = sock.recv(length - len(data)).decode()
if not more:
raise EOFError('socket closed %d bytes into a %d-byte message' %
(len(data), length))
data += more
return data
#if sys.argv[1:] == ['server']:
if True:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(1)
while True:
print('Listening at', s.getsockname())
sc, sockname = s.accept()
print('We have accepted a connection from', sockname)
print('Socket connects', sc.getsockname(), 'and', sc.getpeername())
message = recv_all(sc, 16)
print('The incoming sixteen-octet message says', repr(message))
sc.sendall('Farewell, client'.encode())
This is the rest from srver-side and i can't get working it from here , in rest everything works perfect! ....
if repr(message) == 'exit':
sc.close()
print('Reply sent, socket closed')
else:
print(sys.stderr, 'usage: tcp_local.py server|client [host]')
And this code for the client
import socket, sys
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# HOST = sys.argv.pop() if len(sys.argv) == 3 else '127.0.0.1'
HOST = 'localhost'
PORT = 8080
def recv_all(sock, length):
data = ''
while len(data) < length:
more = sock.recv(length - len(data)).decode()
if not more:
raise EOFError('socket closed %d bytes into a %d-byte message'
% (len(data), length))
data += more
return data
#elif sys.argv[1:] == ['client']:
if True:
print('Connecting to server on: ',HOST,' Port: ',PORT)
s.connect((HOST, PORT))
print('Client has been assigned socket name', s.getsockname())
s.sendall('Hi there, server'.encode())
print('Hi there, server, has been send')
reply = recv_all(s, 16)
print('The server said', repr(reply))
command = (input('enter:').encode())
s.sendall(command)
if s.send(command) == 'exit':
s.close()
else:
print(sys.stderr, 'usage: tcp_local.py server|client [host]')
I can get the client to ask the user for input , and when I enter the string 'exit' (without the quotation marks) in the client side I don't know how to make the server read the raw text and convert it into close server function ... any help would be greatly appreciated ... have a nice Sunday everyone !
I get "Type error : 'str' does not support the buffer interface"
Everythong works fine now , I've added the .encode() at the command = (input('enter:').encode()) part . but how do i make the server to close after this ?

I am not sure of what I am saying but for what I know, if you are using python 3.x, you need to convert string to bytes before sending to through an socket. On the client you should do command = bytes(input('enter:'), "utf-8") and on the server you have to put it back as string so, message = recv_all(sc, 16).decode("utf-8"). Not tested...

Related

python socket / how do you detect disconnection from clients and detect number of connections per user

this is for server authentication for my application.
(im working on login function so dont mind about it)
what i wanna do is to make server receive heartbeat from client
and close client socket if its doesnt respond in a few min
also i want to detect number of connections per user.
for receiving heartbeat, i can make the client send heartbeat constantly but
how do you make the server decect it? i know time measurement is needed but
if i put time.perf_counter() right before 'client_socket.recv(1024)' the counter function wont be executed because its waiting on receiving. so how would i solve this?
and im also trying to make it detect number of connections per user. (5 maximum connections per user) for detection, i give username + 1 when a user is connected and give -1 when the user disconnects but im not sure if the method im doing is correct or a good way to do so.
i'd be appreciated if you could help me out
------------------------server----------------------------
import socket
from _thread import *
import sys
import time
username = ['test123', 'hongengi']
userconnect= 0
def threaded(client_socket, addr):
print('Connected by :', addr[0], ':', addr[1])
while True:
try:
data = client_socket.recv(1024)
print (data.decode())
print('Received from ' + addr[0],':',addr[1] , data.decode())
if data.decode() == ".": # heartbeat
heartbeat = time.perf_counter()
print ("heartbeat")
if data.decode() == "test123":
print ("login success")
userconnect == userconnect + 1
if not data:
print ("no data / disconnect ")
print('Disconnected by ' + addr[0],':',addr[1])
userconnect == userconnect - 1
break
client_socket.send(data)
except (ConnectionResetError, socket.error) as e:
print ("error occurs")
print('Disconnected by ' + addr[0],':',addr[1])
userconnect == userconnect - 1
break
client_socket.close()
HOST = '127.0.0.1'
PORT = 5000
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen()
print('server start')
while True:
print('wait')
client_socket, addr = server_socket.accept()
start_new_thread(threaded, (client_socket, addr))
server_socket.close()
------------------------client----------------------------
import socket
SERVER_IP = 'localhost'
SERVER_PORT = 5000
SIZE = 100
SERVER_ADDR = (SERVER_IP, SERVER_PORT)
heartbeat = "."
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(SERVER_ADDR)
#username = "test123"
#userpass = "123123"
while True:
client_socket.send(heartbeat.encode())
msg = client_socket.recv(SIZE)
print (msg.decode())
One end of a socket is never "notified" when the other socket closes. There is no direct connection, so the only way to tell this is to time out. You can use socket.timeout to establish a timeout time. Your recv will then return with 0 bytes, and that's an indication that your timeout expired.
How to set timeout on python's socket recv method?

Python socket hangs on connect/accept

I'm trying to send packets using sockets, and was able to do so just fine until this morning. I'm not sure what's going on. The packets are showing up in tcpdump but the server and the client cannot connect to each other.
netcat.py
import socket
import argparse
import sys
import os
import re
import threading
def convertContent(content: str = "") -> bytes:
byteContent = []
# grab the hex from the content
for i in range(len(content)):
if content[i] == "\\" and content[i+1] == "x":
byteContent.append(f"{content[i+2]}{content[i+3]}")
# grab the non hex from the content, split it on the hex
stringContent = re.split(r"\\x.{2}", content)
byteIndex = 0
newContent = b""
# Re add the non-hex content, and the hex content
for word in stringContent:
newContent += word.encode()
if byteIndex < len(byteContent):
newContent += bytes.fromhex(byteContent[byteIndex])
byteIndex += 1
newContent = newContent.replace(b"\\n", b"\n").replace(b"\\r", b"\r")
return newContent
class Netcat():
'''
Netcat class that can be used to send/receive TCP packets
'''
BUFFER_SIZE = 1024
def __init__(self):
pass
#classmethod
def createSocket(cls):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Address might be in a TIME_WAIT status, ignore this
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Port might be in a TIME_WAIT status, ignore this
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
return sock
#classmethod
def send(cls, hostname: str = "127.0.0.1", srcPort: int = 0, destPort: int = 9999, content: str = "", buffer_size: int = 1024):
srcPort = int(srcPort)
destPort = int(destPort)
try:
content = convertContent(content=content)
except:
pass
sock = cls.createSocket()
# Set the source port before sending
sock.connect((hostname, destPort))
sock.sendall(content)
# shutdown might be redundant/unnecessary (tells connected host that we're done sending data)
sock.shutdown(socket.SHUT_WR)
while True:
data = sock.recv(buffer_size)
if len(data) == 0:
break
sock.close()
#classmethod
def receive(cls, port: int = 9999, buffer_size: int = 1024):
if port <= 1024 and os.geteuid() != 0:
print(f"Listening on port {port} requires superuser privileges!")
return
host = ""
sock = cls.createSocket()
sock.bind((host, port))
sock.listen(10)
conn, addr = sock.accept()
while True:
data = conn.recv(buffer_size)
if not data:
break
conn.close()
threading.Thread(target=Netcat.receive,daemon=True).start()
Netcat.send(content="test")
Note: I am sending the packets from one VM to another, rather than sending to myself, but it would be a lot to ask people to spin up a bunch of VMs to reproduce this. The hostname param in the send method should be the actual IP of the receiving machine
I've thrown some print statements, and the server stops on sock.accept(), while the client hangs on sock.connect((hostname, destPort))
I checked the hostname for the server, and it's listening on (0.0.0.0, 8888) (assuming 8888 is the port param), which means its listening on all interfaces on that port, so I dont know why its refusing to connect
I tcpdumped on the server, and its getting the packets, it gets a SYN, then sends out a SYN, ACK, but the rest of the packets are marked as re-transmissions.
I've tried looping the accept & connect lines, thinking maybe some sort of race condition was occurring, but no matter what I do the client can't connect to the server.
Edit: This works on my local machine, but still breaks when I try to send packets over the network. The first 2 steps of the handshake go through SYN & SYN, ACK, but not the third ACK
Don't bind in the client. Working example below, but minor changes to make a standalone script:
import socket
import threading
def receive(port: int = 9999, buffer_size: int = 1024):
host = ""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Address might be in a TIME_WAIT status, ignore this
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((host, port))
sock.listen()
conn, addr = sock.accept()
while True:
data = conn.recv(buffer_size)
if not data:
break
print(data)
conn.close()
def send(hostname: str = "127.0.0.1", destPort: int = 9999, content: str = b"test", buffer_size: int = 1024):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Address might be in a TIME_WAIT status, ignore this
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Removed bind
sock.connect((hostname, destPort))
sock.sendall(content)
# shutdown might be redundant/unnecessary (tells connected host that we're done sending data)
sock.shutdown(socket.SHUT_WR)
while True:
data = sock.recv(buffer_size)
if len(data) == 0:
break
sock.close()
threading.Thread(target=receive,daemon=True).start()
send()
Output:
b'test'

How to make client and server send and accept different message lengths in python

In my python homework, I have to make a server and some clients.
My problem comes from the fixed string size in the packing/unpacking process on both the server and client sides. I want to send messages with two different sized strings.
Here is my simplified code:
client:
import socket
import struct
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect(('127.0.0.1', 5555))
str1 = b"A"
msg = (str1, 3)
msg_packed = struct.Struct("1s I").pack(*msg) #the fixed string size is not a problem here
sock.sendall(msg_packed)
reply_packed = sock.recv(1024)
reply = struct.Struct("2s I").unpack(reply_packed) #since the string in the reply can be 'Yes' or 'No' what is 2 and 3 character. I don't know hot make it accept both.
print(reply)
and the Server:
import socket
import select
import struct
srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
srv.bind(('0.0.0.0', 5555))
srv.listen()
socks = [srv]
while True:
readable, writeable, err = select.select(socks, [], [], 0.1)
for s in readable:
if s == srv:
client, client_address = srv.accept()
print("New client from: {} address".format(client_address))
socks.append(client)
else:
msg_packed = s.recv(1024)
if msg_packed:
for sock in socks:
if sock == s and sock != srv:
msg = struct.Struct("1s I").unpack(msg_packed)
if (msg[0] == b'A'): #In here the reply message need to be 'Yes' or 'No'
reply = (b'Yes', msg[1] * msg[1])# the struct.Struct("2s I").pack(*reply) will not going to accept this
else:
reply = (b'No', msg[1] + msg[1])
reply_packed = struct.Struct("2s I").pack(*reply)
sock.send(reply_packed)
else:
print("Client disconnected")
socks.remove(s)
s.close()
Is there any way to be able to send both 2 and 3 string lengths? And if yes, how should I change my code?
EDIT: You can just dynamically set the format string of the struct. Here is a simple example:
str1 = b"Yes"
str2 = b"No"
msg_packed1 = struct.Struct("{}s".format(len(str1))).pack(str1)
msg_packed2 = struct.Struct("{}s".format(len(str2))).pack(str2)
In your example it would be
reply_packed = struct.Struct("{}s I".format(len(reply[0]))).pack(*reply)
I got this idea from packing and unpacking variable length array/string using the struct module in python

python socket programming TypeError: bytes like object is required not str

I have a Python server running that listen to data that is sent by the Python client. The client takes input from the user and sends it to the server which prints it. However, I get the error that says "TyperError: a byte-like object is required, not 'str'". It is on line number 8 of the client code.
SERVER CODE:
import socket
def server(interface, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((interface, port))
sock.listen(1)
print('Listening at', sock.getsockname())
while True:
sc, sockname = sock.accept()
print('We have accepted a connection from', sockname)
print(' Socket name:', sc.getsockname())
print(' Socket peer:', sc.getpeername())
message = sc.recv(1024)
print(' Incoming sixteen-octet message:', repr(message))
sc.sendall(b'Farewell, client')
sc.close()
print(' Reply sent, socket closed')
if __name__ == '__main__':
server('0.0.0.0', 9999)
CLIENT CODE:
import socket
def client(host, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
print('Client has been assigned socket name', sock.getsockname())
command = input("message > ")
sock.sendall(command)
reply = sock.recv(1024)
print('The server said', repr(reply))
sock.close()
if __name__ == '__main__':
client('localhost', 9999)
TyperError: a byte-like object is required, not 'str'
socket.sendall expects bytes, but you have passed in the result of input("message > "), which is a str. So, you need to somehow convert the str from the input into bytes. You can use encode to do this:
command = input("message > ").encode()
encode, well, encodes a str into bytes.
While communicating in Python, you should encode the parameter you send as a byte and decode the parameter you received from the byte format. You can understand what I want to say from the code below.
Server Code:
import socket
def server(interface, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((interface, port))
sock.listen(1)
print('Listening at', sock.getsockname())
while True:
sc, sockname = sock.accept()
print('We have accepted a connection from', sockname)
print(' Socket name:', sc.getsockname())
print(' Socket peer:', sc.getpeername())
message = sc.recv(1024).decode('utf-8')
print(' Incoming sixteen-octet message:', repr(message))
sc.sendall(bytes('Farewell, client','utf-8'))
sc.close()
print(' Reply sent, socket closed')
if __name__ == '__main__':
server('0.0.0.0', 9999)
Client Code:
import socket
def client(host, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
print('Client has been assigned socket name', sock.getsockname())
command = input("message > ")
sock.sendall(bytes(command,'utf-8'))
reply = sock.recv(1024).decode('utf-8)
print('The server said', repr(reply))
sock.close()
if __name__ == '__main__':
client('localhost', 9999)
If you edit your code this way, I think you will correct the error you received. I hope it helps as I am new to this. Sorry for my entry-level English.

UDP TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

I'm completely newbie to python and computer networking. While working on Uni project I have faced a problem. What am I doing wrong? Any help will me much appreciated.
Here is server side:
import socket
def Main():
host = "127.0.0.1"
port = 5000
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, port))
print ("Server Started.")
while True:
data, addr = s.recvfrom(1024)
print ("message from: ") + str(addr)
print ("from connected user: ") + str(data.decode('utf-8'))
data = str(data).upper()
print ("sending: ") + str(data)
s.sendto(data, addr)
s.close()
if __name__ == '__main__':
Main()
Here is my client side:
import socket
def Main():
host = "127.0.0.1"
port = 5000
server = ('127.0.0.1', 5000)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, port))
message = input('->')
while message != 'q':
s.sendto(message.encode('utf-8'), server)
data, addr = s.recvfrom(1024)
print ('Received from server: ') + str(data)
message = input('->')
s.close()
if __name__ == '__main__' :
Main()
There were a couple of issues; mostly with the printing.
You had a few instances of print('some text') + str(data); this won't work, because while print() outputs to the screen (STDOUT) it returns None, so what you were actually doing was trying to concatenate None + str(data)
What you need is print('some text' + str(data)).
Additionally, there was as issue on the server-side where you echo the data received from the client back to the client- it needed to be re-encoded as a bytearray (it comes in as a bytearray, gets converted to a utf-8 string for display, it needs to go back to bytearray before replying).
In summary, server:
import socket
def Main():
host = "127.0.0.1"
port = 5000
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, port))
print("Server Started.")
while True:
try:
data, addr = s.recvfrom(1024)
print("message from: " + str(addr)) # moved string concatenation inside print method
print("from connected user: " + str(data.decode('utf-8'))) # moved string concatenation inside print method
data = str(data).upper()
print("sending: " + str(data)) # moved string concatenation inside print method
s.sendto(data.encode('utf-8'), addr) # needed to re-encode data into bytearray before sending
except KeyboardInterrupt: # added for clean CTRL + C exiting
print('Quitting')
break
s.close()
if __name__ == '__main__':
Main()
And the client:
import socket
def Main():
host = "127.0.0.1"
port = 5001
server = ('127.0.0.1', 5000)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, port))
message = input('->')
while message != 'q':
try:
s.sendto(message.encode('utf-8'), server)
data, addr = s.recvfrom(1024)
print('Received from server: ' + str(data)) # moved string concatenation inside print method
message = input('->')
except KeyboardInterrupt: # added for clean CTRL + C exiting
print('Quitting')
break
s.close()
if __name__ == '__main__':
Main()

Categories