Python Forum

Full Version: Simple TCP Client and TCP Server Problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
OK, I may be being particularly thick with this question but I am new with python and I am trying to follow a PDF book on coding with it, we have reached the point where he has had me create a simple TCP Server (named: tcp_server.py)
import socket
import threading

bind_ip = "0.0.0.0"
bind_port = 9999

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server.bind((bind_ip,bind_port))

server.listen(5)

print ("[*] Listening on %s:%d" % (bind_ip,bind_port))

#this is our client handling thread
def handle_client(client_socket):
    
    #print out what the client sends
    request = client_socket.recv(1024)
    

    print ("[*] Received: %s" % request)
    
    # send back a packet
    client_socket.send("ACK!")
    
    client_socket.close()
    
while True:
    
    client,addr = server.accept()
    
    print ("[*] Accepted connection from: %s:%d" % (addr[0],addr[1]))
    
    #spin up our clirnt thread to handle incoming data
    client_handler = threading.Thread(target=handle_client,args=(client,))
    client_handler.start()
and a simple TCP Client (named: tcp_client.py)
import socket

target_host = "www.google.com"
target_port = 80

# create a socket object
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# connect the client
client.connect((target_host,target_port))

# send some data
client.send('GET / HTTP/1.1\r\nHost: google.com\r\n\r\n')

# receive some data
response = client.recv(4096)

print (response)
in the book he says if I pass the client to the server as an argument then I should get responses like this

Quote:[*] Listening on 0.0.0.0:9999
[*] Accepted connection from: 127.0.0.1:62512
[*] Received: ABCDEF

I have tried passing the filename to many places in the server code but all I ever end up with is
Quote:[*] Listening on 0.0.0.0:9999

and the command windows locks out and I have to close it down, could someone point me in the right direction, I dont want to stray too far away from the book with the coding as the rest of it may not make sense, but where and how would I pass the client filename into the server file for it to do what he says it should do?

many thanks in advance and stay safe
If you set target_host = "localhost" and target_port = 9999 it should work.
Otherwise, your server doesn't get the request because your host is not www.google.com.
P.S. I am using Python 2.7 on Linux

the question I should have reiterated is where in the server file do I place the name of the client file to make it function?
You should avoid the use of Python 2.7. It's deprecated.
Use the right host. If you're accessing www.google.com it will not connect to your server.
thanks for the answers so far DeaD_EyE, apart from using a deprecated version of Python, the real question was where do I put the filename of the client into the server code to make it function?

in fact I going to stop here, upgrade to version 3 of python and start off on the right footing, I will have only lost a day and a half to 2.7, so not such a bad thing, thanks for the help though
You don't put filenames anywhere. TCP is a network protocol and the way you specify where to connect to is to specify the host and port of the server. The host should be either an IP address, or the hostname. Right now, your client is connecting to www.google.com on port 80 (lines 3-10). Your server is running on your machine on port 9999 (lines 5-9) and is accessible on all IP addresses on your machine (line 4). DeaD_Eye has given you the right information - "localhost" is a special hostname meaning your machine (you could also use the IP address 127.0.0.1) and the port is 9999 as in the server code.

Which book are you using? Does the author not cover the basics of what TCP is and how it works?