Answers for "tcp server python"

1

python tcp server

####### TCP Server #######

from socket import AF_INET,SOCK_STREAM,socket

# AF_INET => TCP
# SOCK_STREAM => IPv4

sobj = socket(AF_INET,SOCK_STREAM)
sobj.bind(('127.0.0.1',12345))
sobj.listen(1)

client , addr = sobj.accept()
print("the ip is connected to server ",addr)

while(True):
    
    message = input()
    if(message == "exit"):
        client.close()
        break
    else:
        client.send(message.encode())

        
####### TCP Client #######

from socket import AF_INET,SOCK_STREAM,socket

sobj = socket(AF_INET,SOCK_STREAM)
sobj.connect(('127.0.0.1',12345))
while True:
    message = sobj.recv(2048)
    if(message == "exit"):
        sobj.close()
        break
    elif(message.decode() == ''):
        pass
    else:
        print(message.decode())
Posted by: Guest on August-25-2021
1

tcp python

#!/usr/bin/env python3

import socket

HOST = '127.0.0.1'  # The server's hostname or IP address
PORT = 65432        # The port used by the server

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    s.sendall(b'Hello, world')
    data = s.recv(1024)

print('Received', repr(data))
Posted by: Guest on June-09-2021
6

socket programming in python

#!/usr/bin/env python3

import socket

HOST = '127.0.0.1'  # Standard loopback interface address (localhost)
PORT = 65432        # Port to listen on (non-privileged ports are > 1023)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    conn, addr = s.accept()
    with conn:
        print('Connected by', addr)
        while True:
            data = conn.recv(1024)
            if not data:
                break
            conn.sendall(data)
Posted by: Guest on August-26-2020
2

tcp client using sockets in python

from socket import *
import sys
import json


serverName = ""
serverPort = 6100


clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName, serverPort))

sample_data = {
	"Aparna" : 1,
	"Pooja" : 2,
	"Shreya" : 3,
	"Tanishq" : 4
}

serialized_data = json.dumps(sample_data) #data serialized

# clientSocket.send(str.encode(sample_data))
clientSocket.send(str.encode(serialized_data))

response_data = clientSocket.recv(1024)
print("Response data from server : ", response_data.decode())

clientSocket.close()
Posted by: Guest on November-15-2020
0

tcp python

conn, addr = s.accept()
with conn:
    print('Connected by', addr)
    while True:
        data = conn.recv(1024)
        if not data:
            break
        conn.sendall(data)
Posted by: Guest on June-09-2021
-2

client server python socket

s = socket.socket (socket_family, socket_type, protocol=0)
Posted by: Guest on December-15-2020

Python Answers by Framework

Browse Popular Code Answers by Language