Search on blog:

Socket: send and receive at the same time

One socket may send and receive at the same time but one thread has to only send data and other thread has to only receive data. This way send() doesn't have to wait for the end of recv() and recv() doesn't have to wait for the end of send() so they not block each other.

Without threads it would be harder to do this.

client.py

import socket
import threading
import sys

# --- functions ---

def recv_msg():
    while True:
        recv_msg = conn.recv(1024)
        if not recv_msg:
            sys.exit(0)
        recv_msg = recv_msg.decode()
        print(recv_msg)

def send_msg():
    while True:
        send_msg = input(str("Enter message: "))
        send_msg = send_msg.encode()
        conn.send(send_msg)
        print("message sent")

# --- main ---

host = socket.gethostname()
port = 8080

s = socket.socket()
s.bind((host, port))
s.listen(1)

print("Waiting for connections")
conn, addr = s.accept()

print("Client has connected")
conn.send("Welcome to the server".encode())

# thread has to start before other loop
t = threading.Thread(target=recv_msg)
t.start()

send_msg()

server.py

import socket
import threading
import sys

# --- functions ---

def recv_msg():
    while True:
        recv_msg = conn.recv(1024)
        if not recv_msg:
            sys.exit(0)
        recv_msg = recv_msg.decode()
        print(recv_msg)

def send_msg():
    while True:
        send_msg = input(str("Enter message: "))
        send_msg = send_msg.encode()
        conn.send(send_msg)
        print("message sent")

# --- main ---

host = socket.gethostname()
port = 8080

s = socket.socket()
s.bind((host, port))
s.listen(1)

print("Waiting for connections")
conn, addr = s.accept()

print("Client has connected")
conn.send("Welcome to the server".encode())

# thread has to start before other loop
t = threading.Thread(target=recv_msg)
t.start()

send_msg()

Python: Jak wysłać request HTTPS z użyciem PySocks

Aby połączyć się za pomocą szyfrowane HTTPS potrzebny jest standardowy moduł ssl do stworzenia wrapper na oryginalnym socket.

import socks
import ssl

s = socks.socksocket()

#s.set_proxy(socks.SOCKS5, "localhost", 9050)  # TOR proxy server

s.connect(("httpbin.org", 443))
ss = ssl.wrap_socket(s, keyfile=None, certfile=None, server_side=False, cert_reqs …

« Page: 1 / 1 »