[Tutor] Fwd: Tutor Digest, Vol 131, Issue 14

Alan Gauld alan.gauld at btinternet.com
Sat Jan 3 19:07:55 CET 2015


Forwarded to list for info.

Please use ReplyAll in responses to the tutor list.
And please do not quote the entire digest,
delete any irrelevant parts.

On 03/01/15 16:00, pramod gowda wrote:
> Hello ,
>
> I have chaged the code as per your input, except port number.
> If i change the port number it give me error as
>
> Traceback (most recent call last):
>   File "C:\Users\Pramod\Desktop\client.py", line 7, in <module>
> client_socket.connect((server_address,server_port))
> ConnectionRefusedError: [WinError 10061] No connection could be made 
> because the target machine actively refused it
>
Interesting. I assumed you changed it at both ends?
Does the server have a firewall running on it?
If so you may need to open the port.
> Server code:
>
> import socket
>
> server_socket=socket.socket()
> server_name='192.168.2.2'
> server_port= 80
> server_socket.bind((server_name,server_port))
> server_socket.listen(5)
>
> while True:
>     print("hello")
>     c,address=server_socket.accept()
>     print("acception the connection")
>     print("we got connection from:",address)
>     c.send("hello,hw ru")
>     c.close()
>
> Client Code:
>
> import socket
>
> client_socket=socket.socket()
> server_address='192.168.2.2'
> server_port= 80
> print("port number entered")
> client_socket.connect((server_address,server_port))
> print("it is connected")
> data=client_socket.recv(1024)
> print("received:",data)
> client_socket.close()
> print('finished')
>

> port number entered
> it is connected
> received: *b''*
> finished
>

Looks like you are receiving a bytestring in Python 3
(which is what I'd  expect) so you may need to decode()
it to get a text string

> In the above output expected data is 'hello,hw ru' from the server and
>  print("acception the connection")
>   print("we got connection from:",address)
>
> the above print statements are not executed.
>

Really? You don't see the server output? That suggests your client
is not connecting. Can you connect via telnet?

I just tried your server code and get a typeError exception.
You need to send a bytestring like so:

c.send(b"Hello, hw ru")

You can then test your server using telnet with

telnet <host> <port>



-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list