Questions tagged [python-sockets]

For questions related to the sockets module in the Python standard library, or more broadly socket programming in Python.

The Python library comes with a HOWTO document for sockets programming:

Socket Programming HOWTO — Python 3.x documentation

The standard library is well-documented:

socket - Low-level networking interface

Like the title suggests, sockets are the basic facility for network connections, and on Unix, several other types of interprocess connections. Beginners who want to use a particular application-level protocol should often try to find an existing higher-level library for that instead of programming directly with sockets.

506 questions
92
votes
1 answer

Python requests speed up using keep-alive

In the HTTP protocol you can send many requests in one socket using keep-alive and then receive the response from server at once, so that will significantly speed up whole process. Is there any way to do this in python requests lib? Or are there any…
PaulOverflow
  • 1,091
  • 1
  • 10
  • 12
26
votes
3 answers

What is the function of SOCK_STREAM?

I am learning about sockets in Python and came up with variable = socket.socket(socket.AF_INET, socket.SOCK_STREAM) I understood the function of this socket.socket and socket.AF_INET but I am curious about socket.SOCK_STREAM. What is its function?
Shashank Pulijala
  • 361
  • 1
  • 3
  • 3
15
votes
6 answers

python socket GET

From the other posts on stack overflow this should be working import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("www.cnn.com" , 80)) s.sendall("GET / HTTP/1.1\r\n") print s.recv(4096) s.close but for…
james smith
  • 227
  • 1
  • 2
  • 7
14
votes
1 answer

Difficulty using Python's socket.gethostbyaddr()

I am trying to reverse dns a list of IPs using socket.gethostbyaddr() in python, which returns 'Unknown Host' for some values, but using dig for the same ip returns the Hostname. Also, dig seems to be significantly faster than using python module,…
bilkulbekar
  • 327
  • 2
  • 3
  • 10
10
votes
6 answers

Getting 127.0.1.1 instead of 192.168.1.* ip ubuntu python

I am new to python. I want to get the ipaddress of the system. I am connected in LAN. When i use the below code to get the ip, it shows 127.0.1.1 instead of 192.168.1.32. Why it is not showing the LAN ip. Then how can i get my LAN ip. Every…
Smack Alpha
  • 1,828
  • 1
  • 17
  • 37
9
votes
1 answer

Python Requests, how to bind to different source ip for each request?

I'm trying to learn some python, and i'm having issues with the logic in what I want to test. Currently my code is written in a way that binding to source_address doesn't change when the process starts import socket import requests real_create_conn…
Marie Anne
  • 301
  • 1
  • 2
  • 12
7
votes
2 answers

When/why to use s.shutdown(socket.SHUT_WR)?

I have just started learning python network programming. I was reading Foundations of Python Network Programming and could not understand the use of s.shutdown(socket.SHUT_WR) where s is a socket object. Here is the code(where sys.argv[2] is the…
shiva
  • 2,535
  • 2
  • 18
  • 32
6
votes
2 answers

Permanent gaierror 'Temporary failure in name resolution' after running for a few hours

I have a long running python script, launched with upstart. This script makes quite a lot of requests. Everything works well at first, however after a few hours I start permanently getting the following error for each request: File…
lipka
  • 1,162
  • 11
  • 19
6
votes
0 answers

Frontend raw TCP/UDP sockets in a browser, any way at all in 2021?

I've seen many questions about using raw TCP/UDP sockets in web browsers using JavaScript/Html5 however they are all old like this one posted 5 years ago. It is now 2021 and I'm wondering if there is any possibility at all to avoid using WebSockets…
NaturalBornCamper
  • 3,675
  • 5
  • 39
  • 58
5
votes
1 answer

asyncio doesn't send the entire image data over tcp

I am trying to send an image from my local computer to a computer in the cloud using asyncio with TCP protocol. Sometimes I get the entire image being sent and sometimes only part of the image gets sent. client code import…
ahat
  • 343
  • 3
  • 9
5
votes
3 answers

trying to send HTTP response from low level socket server

This is my code: # Process connections print('Listening on port', port) while True: c, addr = s.accept() print("Got connection from", addr) msg = "" response_headers = { 'Content-Type': 'text/html;…
dopatraman
  • 13,416
  • 29
  • 90
  • 154
5
votes
3 answers

How to check if a specific port is listening using Python script?

I want to check if api and app are running before running tests on them. I know I can get a list of open ports in CLI using sudo lsof -iTCP -sTCP:LISTEN -n -P But I want to write a python script to do so. Any ideas on what library should I use or…
Mahsa Mortazavi
  • 755
  • 3
  • 7
  • 23
5
votes
5 answers

Finding Live Nodes on LAN using Python

I am creating a Messenger which is same as IP Messenger in Python 2.7 and Windows. I want the same functionality as IP Messenger uses in finding the systems running same software over LAN but I am unable to understand the technique. Can someone…
aki92
  • 2,064
  • 2
  • 14
  • 12
5
votes
2 answers

python: loop with socket.recv()

Do you know why this loop doesn't break? #!/usr/bin/env python from socket import * import os import sys if __name__ == '__main__': HOST = '127.0.0.1' PORT = 55554 print 'Creating socket' socketProxy = socket(AF_INET, SOCK_STREAM) print…
SagittariusA
  • 5,289
  • 15
  • 73
  • 127
4
votes
1 answer

How do i catch a SSL/TLS key using python sockets on server side

The client makes a GET request on some websites, like https://www.youtube.com. This request is redirected to my proxy server, which gets all of the data. Then i try to decode('UTF-8') it, but i get error UnicodeDecodeError: 'utf-8' codec can't…
1
2 3
33 34