6 Replies - 3276 Views - Last Post: 19 September 2011 - 04:03 PM Rate Topic: -----

#1 Dev1462  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 20-May 10

Python server and client problem

Posted 19 September 2011 - 11:19 AM

Hey guys i cant figure out what is going on with the server/client i made from my python book, here is the source code, you will probably already know what is wrong with it. I get this error when i run the server and then run the client...

Quote

Traceback (most recent call last):
File "C:/Python32/minclient.py", line 9, in <module>
s.connect((host, port))
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it


Server.py
import socket

s = socket.socket()

host = socket.gethostname()

port = 1234

s.bind((host, port))

s.listen(5)

while True:
    c, addr = s.accept()
    print('Got connection from', addr)
    c.send('Thank you for connecting')
    c.close()



Client.py
import socket

s = socket.socket()

host = socket.gethostname()

port = 1234

s.connect((host, port))

print(s.recv(1024))



Is This A Good Question/Topic? 0
  • +

Replies To: Python server and client problem

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Python server and client problem

Posted 19 September 2011 - 11:25 AM

In your server, try setting the host to "0.0.0.0". This will force the socket to listen on any interface.

I've never seen a socket bind to a host name, and I'm not sure how this would work out. Typically, you would set this to the IP address of the interface you wanted to listen on.

This post has been edited by Motoma: 19 September 2011 - 11:25 AM

Was This Post Helpful? 0
  • +
  • -

#3 Dev1462  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 20-May 10

Re: Python server and client problem

Posted 19 September 2011 - 11:28 AM

ok i set it to host = "0.0.0.0"

but it still gave me the same error :/

well this is just for a local test to practice sockets for the first time. Do you have an alternative that works? I'm just copying what was from Beginning Python: Novice to Professional from APRESS

This post has been edited by Dev1462: 19 September 2011 - 11:30 AM

Was This Post Helpful? 0
  • +
  • -

#4 Dev1462  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 20-May 10

Re: Python server and client problem

Posted 19 September 2011 - 12:04 PM

Ok, so i changed my python to version 2.7 and now i dont get any weird errors like that, however i cannot use recv() for some reason, when i do i get no information back, here are my modified files:

server.py:
import socket

s = socket.socket()

host = socket.gethostname()

port = 1234

s.bind((host, port))

s.listen(5)

while True:
    print 'Now looking to accept the client'
    c, addr = s.accept()
    print 'Got connection from:', addr
    c.send('Hello i am the server')
    c.close()



client.py
import socket

s = socket.socket()

host = socket.gethostname()

port = 1234

s.connect((host, port))
text = s.recv(1024)
print text

print 'Information received'



when i run the code this is what it looks like in idle:

Quote

>>>Now looking to accept the client

>>>Information received

but i do not get the information that i ask the server to do once a client connects to the server
Was This Post Helpful? 0
  • +
  • -

#5 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Python server and client problem

Posted 19 September 2011 - 12:54 PM

Odd, that code all works for me.

Server output:
Now looking to accept the client
Got connection from: ('127.0.0.1', 51655)
Now looking to accept the client



Client output:
Hello i am the server
Information received


Was This Post Helpful? 0
  • +
  • -

#6 Dev1462  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 20-May 10

Re: Python server and client problem

Posted 19 September 2011 - 02:04 PM

That's so weird. I even set IDLE to run as admin to see if that was the problem. I changed the host to 127.0.0.1 and port to 56155 but nothing ever gets sent :/ I've tried doing this on my home computer and my laptop and still it does not work. I'm running windows 7 professional. And an AMD processor

Wait i had a theory, could the programs be talking but IDLE just wont show it? What are you using to build and run your python scripts?

Yup my theory stands, i tested the .py files in the cmd window and they both communicated, IDLE is just a piece of s^*t

This post has been edited by Dev1462: 19 September 2011 - 02:29 PM

Was This Post Helpful? 0
  • +
  • -

#7 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Python server and client problem

Posted 19 September 2011 - 04:03 PM

Are you using the right version of IDLE? Each installation of Python comes with an IDLE running that version of Python. You can check at the top of the IDLE window when you start it up.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1