Last active
December 28, 2021 10:22
-
-
Save xirixiz/809ad4a7c6115727bfce19d53a04c717 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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() |
Author
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.
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
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
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