Best way to open a socket in Python

I want to open a TCP client socket in Python. Do I have to go through all the low-level BSD create-socket-handle / connect-socket stuff or is there a simpler one-line way?


Asked by: Cherry847 | Posted: 24-09-2021






Answer 1

Opening sockets in python is pretty simple. You really just need something like this:

import socket
sock = socket.socket()
sock.connect((address, port))

and then you can send() and recv() like any other socket

Answered by: Carlos605 | Posted: 25-10-2021



Answer 2

OK, this code worked

s = socket.socket()
s.connect((ip,port))
s.send("my request\r")
print s.recv(256)
s.close()

It was quite difficult to work that out from the Python socket module documentation. So I'll accept The.Anti.9's answer.

Answered by: Audrey935 | Posted: 25-10-2021



Answer 3

For developing portable network programs of any sort in Python, Twisted is quite useful. One of its benefits is providing a convenient layer above low-level socket APIs.

Answered by: Chelsea815 | Posted: 25-10-2021



Similar questions

networking - Getting Socket Data in Python

(I'm writing this in python) I've been stuck on this for a while now : I am writing a UDP collector(in python) that data is being sent to. However, I cannot get the correct data from it. One of my coworkers has written a UDP collector (in golang) and receives the below output : (key difference will be bolded) %!(EXTRA chan_service.Request={1501844689333211815 0 STATS user-master-01 172.22.64.123 30 true


networking - Getting Socket Data in Python

(I'm writing this in python) I've been stuck on this for a while now : I am writing a UDP collector(in python) that data is being sent to. However, I cannot get the correct data from it. One of my coworkers has written a UDP collector (in golang) and receives the below output : (key difference will be bolded) %!(EXTRA chan_service.Request={1501844689333211815 0 STATS user-master-01 172.22.64.123 30 true


networking - Python - Library Problems

I'm relatively new to Python and am having problems programming with Scapy, the Python network manipulation tool. However, I can't tell if it's as much a Scapy problem as it is a being-a-Python-newbie problem. On the scapy site, they give a sample program which I'm not able to run on my own machine: #! /usr/bin/env...


Good Python networking libraries for building a TCP server?

I was just wondering what network libraries there are out there for Python for building a TCP/IP server. I know that Twisted might jump to mind but the documentation seems scarce, sloppy, and scattered to me. Also, would using Twisted even have a benefit over rolling my own server with select.select()?


networking - Is there a Python library than can simulate network traffic from different addresses

Is there a python library out there than can allow me to send UDP packets to a machine (sending to localhost is ok) from different source addresses and ports? I remember that one existed, but can't find it anymore.


networking - How can I check if an ip is in a network in Python?

Given an ip address (say 192.168.0.1), how do I check if it's in a network (say 192.168.0.0/24) in Python? Are there general tools in Python for ip address manipulation? Stuff like host lookups, ip adddress to int, network address with netmask to int and so on? Hopefully in the standard Python library for 2.5.


networking - Creating connection between two computers in python

The question: How do I create a python application that can connect and send packets over the internet to another computer running the same application? Is there any existing code/library I could use? The background: I am pretty new to programming (HS senior). I've created a lot of simple things in python but I've recently decided to start on a bigger project. I'm considering creating a Magic: the Gathering booster...


networking - Yahoo Chat in Python

I'm wondering how the best way to build a way to interface with Yahoo Chat is. I haven't found anything that looks incredibly easy to do yet. One thought it to build it all from scratch, the other thought would be to grab the code from open source software. I could use something like zinc, however, this maybe more complex than it needs to be. Another option would be to find a library that supports it, however, I haven'...


networking - How do I resolve an SRV record in Python?

Something which doesn't rely on native libraries would be better.


networking - Interface with remote computers using Python

I've just become the system admin for my research group's cluster and, in this respect, am a novice. I'm trying to make a few tools to monitor the network and need help getting started implementing them with python (my native tongue). For example, I would like to view who is logged onto remote machines. By hand, I'd ssh and who, but how would I get this info into a script for manipulation? Something li...


networking - listing network shares with python

if I explicitly attempt to list the contents of a shared directory on a remote host using python on a windows machine, the operation succeeds, for example, the following snippet works fine: os.listdir("\\\\remotehost\\share") However, if I attempt to list the network drives/directories available on the remote host, python fails, an example of which is shown in the following code snippet:


networking - Python socket receive - incoming packets always have a different size

I'm using the SocketServer module for a TCP server. I'm experiencing some issue here with the recv() function, because the incoming packets always have a different size, so if I specify recv(1024) (I tried with a bigger value, and smaller), it gets stuck after 2 or 3 requests because the packet length will be smaller (I think), and then the server gets stuck until a timeout. class ...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top