Jun-08-2020, 02:12 PM
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)
I have tried passing the filename to many places in the server code but all I ever end up with is
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
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 thisQuote:[*] 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
