Skip to content

Instantly share code, notes, and snippets.

@xirixiz
Last active December 28, 2021 10:22
Show Gist options
  • Save xirixiz/809ad4a7c6115727bfce19d53a04c717 to your computer and use it in GitHub Desktop.
Save xirixiz/809ad4a7c6115727bfce19d53a04c717 to your computer and use it in GitHub Desktop.
#!/bin/python2s
# Symbolic name, meaning all available interfaces
VAR_HOST = ""
# Arbitrary non-privileged port
VAR_PORT = 9003
import socket
import sys
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
local_hostname = socket.gethostname()
local_ip = socket.gethostbyname(local_hostname)
# Bind socket to local host and port
try:
s.bind((VAR_HOST, VAR_PORT))
except socket.error as msg:
print "Bind failed. Error Code : " + str(msg[0]) + " Message " + msg[1]
sys.exit()
# Start listening on socket
s.listen(10)
print "Socket now listening on " + local_ip + "/" + local_hostname + " on port " + str(
VAR_PORT
)
# Now keep talking with the client
while 1:
# Wait to accept a connection - blocking call
conn, addr = s.accept()
print "Connection initiated from " + addr[0]
s.close()
#!/bin/python3
# Symbolic name, meaning all available interfaces
VAR_HOST = ""
# Arbitrary non-privileged port
VAR_PORT = 9003
import socket
import sys
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
local_hostname = socket.gethostname()
local_ip = socket.gethostbyname(local_hostname)
# Bind socket to local host and port
try:
s.bind((VAR_HOST, VAR_PORT))
except socket.error as msg:
print("Bind failed. Error Code : " + str(msg[0]) + " Message " + msg[1])
sys.exit()
# Start listening on socket
s.listen(10)
print(
"Socket now listening on "
+ local_ip
+ "/"
+ local_hostname
+ " on port "
+ str(VAR_PORT)
)
# Now keep talking with the client
while 1:
# Wait to accept a connection - blocking call
conn, addr = s.accept()
print("Connection initiated from " + addr[0])
s.close()
@B-Yassine
Copy link

But how to get the data received from the server?

@xirixiz
Copy link
Author

xirixiz commented Dec 28, 2021

http://localhost:9003
It's actually quiet obvious :), especially when looking at line 25.

@B-Yassine
Copy link

But you can't you're not connected when I tried just to print the s.recv(1024) I got this error:

[WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

@xirixiz
Copy link
Author

xirixiz commented Dec 28, 2021

Ah Windows, maybe try this instead:

python3 -m http.server 9003 --bind 127.0.0.1

And I believe it should be conn.recv to read from the accepted client socket, not s.recv to read from the listening socket.

@B-Yassine
Copy link

Yes of course I set the VAR_HOST variable the problem is that you can't call the recv function to receive the data if you don't start a connection

@B-Yassine
Copy link

That's it exactely it should be conn.recv to read from the client socket, not s.recv, thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment