Answers for "python socket receive data"

2

send get request python socket

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 
request = "GET / HTTP/1.1\r\nHost:%s\r\n\r\n" % target_host
client.send(request.encode())  
 
# receive some data 
response = client.recv(4096)  
http_response = repr(response)
http_response_len = len(http_response)
 
#display the response
gh_imgui.text("[RECV] - length: %d" % http_response_len)
gh_imgui.text_wrapped(http_response)
Posted by: Guest on May-27-2020
1

python socket send big data

# Client Side Program (Sender)
import socket

data = b"hsfbhdhfbjsnkdfkkn" * 1000  # Very Big Data to send

server = ('127.0.0.1', 8000)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(server)

block_size = 1000
block_number = 0
block_data = b''

while True:
  # Selected block to send
  block_data = data[block_number * block_size: (block_number + 1) * block_size]
  
  s.send_all(block_data)
  
  if not block_data:
    break
  
  block_number += 1
s.close()
Posted by: Guest on September-19-2020
6

socket python functions

#!/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
5

python socket

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 November-27-2020

Code answers related to "python socket receive data"

Python Answers by Framework

Browse Popular Code Answers by Language