Answers for "socket send python"

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
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
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
0

post request socket python

from socket import socket,error
headers = """GET http://www.google.com HTTP/1.1
                Host: www.google.com\r\n\r\n"""
try:
  s = socket()
  s.connect((host,int(port)))
  s.settimeout(4)
  s.send(headers.encode())
  s.recv(800)
  s.close()
except error:
  s.close()
Posted by: Guest on March-11-2021
0

python socket client

import socket
import threading

HOST = '127.0.0.1'
PORT = 9090

class Client:
    def __init__(self, host, port):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect((host, port))
        
        receive_thread = threading.Thread(target=self.receive)
        receive_thread.start()

    def write(self, message):
        message = message.encode('utf-8')
        self.sock.send(message)

    def stop(self):
        self.sock.close()

    def receive(self):
        while True:
            try:
                message = self.sock.recv(1024).decode('utf-8')
                print(message)
            except ConnectionAbortedError:
                break
            except:
                print("Error")
                self.socket.close()

client = Client(HOST, PORT)
Posted by: Guest on August-24-2021
0

socket hostname

socket.gethostbyname(socket.gethostname())
Posted by: Guest on September-18-2020

Python Answers by Framework

Browse Popular Code Answers by Language