[python]Simple tcp client

So here’s the simple code:

import socket

host = '192.168.1.152'
port = 8080
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect((host,port))
client.send(b'GET / HTTP/1.1\r\nHost: 192.168.1.152\r\n\r\n')
response = client.recv(4096)
print(response)

So first create an object client, the attributes are IPV4 (socket.AF_INET) and tcp (socket.SOCK_STREAM).

Use the send method to send http request, must send the msg in bytes, hence the b.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s