1# Basic example where server accepts messages from client.
2
3# importing socket library
4import socket
5
6# socket.AF_INET means we're using IPv4 ( IP version 4 )
7# socket.SOCK_STREAM means we're using TCP protocol for data transfer
8socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
9
10print("Choose mode. s - server, c - client")
11mode = input("> ")
12
13if mode == "s":
14 ip = input("Your computer's ip address: ")
15 port = 80
16
17 # Binding socket to specified ip address and port
18 socket.bind((ip, port))
19
20 # This is max ammount of clients we will accept
21 socket.listen(1)
22
23 # Accepting connection from client
24 sock, addr = socket.accept()
25 print("Client connected from", addr)
26
27 # Receiving data from client
28 while True:
29 data = sock.recv(16384) # Raw data from client
30 text = data.decode('utf-8') # Decoding it
31
32 print("Message from client:", text)
33
34elif mode == "c":
35 ip = input("Server ip address: ")
36
37 # Connecting to server
38 socket.connect((ip, 80))
39
40 # Sending data
41 while True:
42 socket.send(input("Message: ").encode('utf-8'))
1import socket
2TCP_IP = '127.0.0.1'
3TCP_PORT = 5005
4BUFFER_SIZE = 1024
5MESSAGE = "Hello, World!"
6s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
7s.connect((TCP_IP, TCP_PORT))
8s.send(MESSAGE)
9data = s.recv(BUFFER_SIZE)
10s.close()
11print "received data:", data
1target_host = "www.google.com"
2
3target_port = 80 # create a socket object
4client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
5
6# connect the client
7client.connect((target_host,target_port))
8
9# send some data
10request = "GET / HTTP/1.1\r\nHost:%s\r\n\r\n" % target_host
11client.send(request.encode())
12
13# receive some data
14response = client.recv(4096)
15http_response = repr(response)
16http_response_len = len(http_response)
17
18#display the response
19gh_imgui.text("[RECV] - length: %d" % http_response_len)
20gh_imgui.text_wrapped(http_response)
1####### TCP Server #######
2
3from socket import AF_INET,SOCK_STREAM,socket
4
5# AF_INET => TCP
6# SOCK_STREAM => IPv4
7
8sobj = socket(AF_INET,SOCK_STREAM)
9sobj.bind(('127.0.0.1',12345))
10sobj.listen(1)
11
12client , addr = sobj.accept()
13print("the ip is connected to server ",addr)
14
15while(True):
16
17 message = input()
18 if(message == "exit"):
19 client.close()
20 break
21 else:
22 client.send(message.encode())
23
24
25####### TCP Client #######
26
27from socket import AF_INET,SOCK_STREAM,socket
28
29sobj = socket(AF_INET,SOCK_STREAM)
30sobj.connect(('127.0.0.1',12345))
31while True:
32 message = sobj.recv(2048)
33 if(message == "exit"):
34 sobj.close()
35 break
36 elif(message.decode() == ''):
37 pass
38 else:
39 print(message.decode())
40
41
1#!/usr/bin/env python3
2
3import socket
4
5HOST = '127.0.0.1' # Standard loopback interface address (localhost)
6PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
7
8with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
9 s.bind((HOST, PORT))
10 s.listen()
11 conn, addr = s.accept()
12 with conn:
13 print('Connected by', addr)
14 while True:
15 data = conn.recv(1024)
16 if not data:
17 break
18 conn.sendall(data)
19