Author Topic: Python TCP client/server  (Read 1178 times)

0 Members and 1 Guest are viewing this topic.

Offline Trap_lord

  • /dev/null
  • *
  • Posts: 19
  • Cookies: -20
    • View Profile
Python TCP client/server
« on: February 23, 2016, 06:05:03 pm »
Finally got this project working. TCP client/sever, client is set to send and recieve packets from client and server listens for incoming traffic and sends packets back.

client:
Code: [Select]
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 data
client.send("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")

# receive data
response = client.recv(4096)

print response

server:
Code: [Select]
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)

# thread for handling clients
def handle_client(client_socket):

    #print out what client sends
    request = client_client.recv(1024)

    print "[*] Received: %s" % request

    # send a packet to client
    client_socket.send("ACK!")

    client_socket.close()

while True:

    client,addr = server.accept()

    print "[*] Accepted connection from: %s:%d" % (addr[0],addr[1])

    # spin client thread to handle incoming traffic
    client_handler = threading.Thread(target=handle_client,args=(client,))
    client_handler.start()
This is your life and it's ending one minute at a time - Fight Club(1999)

Offline white-knight

  • Knight
  • **
  • Posts: 190
  • Cookies: 26
    • View Profile
Re: Python TCP client/server
« Reply #1 on: February 23, 2016, 06:25:51 pm »
Isn't this from Black Hat Python? The Networking Socket Module?  ???

It's from blackhat python or violent python . I remember reading it

Offline Trap_lord

  • /dev/null
  • *
  • Posts: 19
  • Cookies: -20
    • View Profile
Re: Python TCP client/server
« Reply #2 on: February 23, 2016, 08:03:06 pm »
Ya it is from black hat python but for some reason it didn't work for me the first time I tried so maybe I did something different this time? Thought I would post it for those who don't have the book and want to do a networking related python project.
This is your life and it's ending one minute at a time - Fight Club(1999)

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: Python TCP client/server
« Reply #3 on: February 23, 2016, 08:19:09 pm »
Why in the name of the frogs am i sending the traffic to Google in the client.

To get serious and show us you wanna learn, i need to see variable ways to enter a target and port in the client, same goes for the server.
Would have done OOP if it was me doing it, wouldn't kill you if you turned this little code into it.
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]