Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Chat (Client-Server)
#1
Hi guys. I have the following code (in Python 3.7.3):

###################################################################################################
### Librerias ###
#################
import socket
import threading
import sys
###################################################################################################
### Servidor ###
################
class Server:
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	connections = []
	def __init__(self):
		self.sock.bind(('0.0.0.0', 10000))
		self.sock.listen(1)

	def handler(self,c,a):
		while True:
			data = c.recv(1024)
			for connection in self.connections:
				connection.send(data)
			if not data:
				print(str(a[0]) + ':' + str(a[1]), "Desconectado")
				self.connections.remove(c)
				c.close()
				break
	def run(self):
		while True:
			c,a = self.sock.accept()
			cThread = threading.Thread(target=self.handler, args=(c,a))
			cThread.daemon = True
			cThread.start()
			self.connections.append(c)
			print(str(a[0]) + ':' + str(a[1]), "Conectado")
###################################################################################################
### Cliente ###
###############
class Client:
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

	def sendMsg(self):
		while True:
			self.sock.send(bytes(input(""),'utf-8'))

	def __init__(self):
		self.sock.connect((address, 10000))

		iThread = threading.Thread(target=self.sendMsg)
		iThread.daemon = True
		iThread.start()
		
		while True:
			data = self.sock.recv(1024)
			if not data:
				break
		print(data)
###################################################################################################
### Ejecucion ###
#################
if (len(sys.argv) > 1):
	client = Client(sys.argv(1))
else:
	server = Server()
	server.run()
###################################################################################################
The Server code runs OK:

[Image: TQetpLI.png]

But the Client code does not:

[Image: s9xKfIg.png]

I am following this Video Tutorial:



Thank you very much for your help.
Reply
#2
sys.argv stores the values in list structure, hence to access it we have use index

###################################################################################################
### Ejecucion ###
#################
if (len(sys.argv) > 1):
    client = Client(sys.argv(1))
Modify that to, client = Client(sys.argv[1])
Reply
#3
(Aug-26-2019, 05:04 AM)Malt Wrote: sys.argv stores the values in list structure, hence to access it we have use index

###################################################################################################
### Ejecucion ###
#################
if (len(sys.argv) > 1):
    client = Client(sys.argv(1))
Modify that to, client = Client(sys.argv[1])

Thank you. Problem solved but now I get another error: Doh

[Image: c0u8gP9.png]
Reply
#4
You are creating an instance of class Client with one argument
client = Client(sys.argv[1])
And your Client class initialization is defined as:
class Client:
    def __init__(self):
        self.sock.connect((address, 10000))
It seems you want to create the Client instance with the IP address
so the __init__ needs to be modified:
class Client:
    def __init__(self, address):
        self.sock.connect((address, 10000))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Paramiko Server -- Exception (server): Error reading SSH protocol banner ujlain 3 4,281 Jul-24-2023, 06:52 AM
Last Post: Gribouillis
  Client/Server proper finalizing transfer wolfman5874 1 1,423 Jul-04-2022, 07:35 PM
Last Post: wolfman5874
Bug Problem connecting TLS client written in C++ and Twisted server gpropf 0 1,361 Jun-12-2022, 05:57 PM
Last Post: gpropf
  Server/client basic communication ebolisa 0 2,009 Sep-30-2021, 12:22 PM
Last Post: ebolisa
  Client server Multithreading Anan 6 5,755 Apr-21-2021, 08:19 PM
Last Post: SheeppOSU
Question Trouble with Client/Server reverse Shell! Gilush 0 2,757 Feb-03-2021, 01:04 PM
Last Post: Gilush
  Basic client server code question swisscheese 4 3,192 Dec-12-2020, 08:51 AM
Last Post: Larz60+
  How can i create a server for already existing client using Python? Chapanson 21 7,320 Aug-19-2020, 09:12 AM
Last Post: DeaD_EyE
  Simple TCP Client and TCP Server Problem Vapulabis 5 4,336 Jul-12-2020, 05:09 PM
Last Post: ndc85430
  how to send an image from server to client using PICKLE module dafdaf 1 3,067 Jun-02-2020, 01:08 PM
Last Post: nuffink

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020