Search
Search titles only
By:
Search titles only
By:
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Forums
New posts
All threads
Latest threads
New posts
Trending threads
Trending
Search forums
What's new
New posts
New ads
New profile posts
Latest activity
Free Ads
Latest reviews
Search ads
Members
Current visitors
New profile posts
Search profile posts
Contact us
Latest ads
එක පැකේජ් එකයි මාසෙටම Unlimited Internet. තාමත් DATA CARD දාන්න සල්ලි වියදම් කරනවද? අඩුම මිලට අපෙන්.
sayuru bandara
Updated:
Tuesday at 12:30 PM
Ad icon
ඉන්ටර්නෙට් එකෙන් හරියටම සල්ලි හොයන්න සහ Success වෙන්න කැමතිද? 🚀 (E-Money & Success Stories)
siri sumana
Updated:
Saturday at 11:44 PM
Gemini AI PRO 18 months Offer
Hawaka
Updated:
May 27, 2026
Ad icon
koko account
DasunEranga
Updated:
May 27, 2026
Ad icon
koko account
DasunEranga
Updated:
May 27, 2026
Electronics
Vehicles
Property
Search
Reply to thread
Forums
General
ElaKiri Talk!
python server code awlak help
Get the App
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
<blockquote data-quote="hecker_thama" data-source="post: 29100768" data-attributes="member: 582558"><p>Me mage code eka</p><p></p><p>[CODE=python]import socket</p><p>import sys</p><p></p><p>#creating socket(Connect with 2 pcs)</p><p>def create_socket():</p><p> try: </p><p> global host</p><p> global port</p><p> global s</p><p></p><p> host = "127.0.0.1"</p><p> port = 9999</p><p> s = socket.socket()</p><p></p><p> except socket.error as msg:</p><p> print("Socket creation error: " + str(msg))</p><p></p><p>#binding the socket and listening for connections</p><p></p><p>def bind_socket():</p><p> try:</p><p> global host</p><p> global port</p><p> global s</p><p></p><p> print("Binding the Port: " + str(port))</p><p></p><p> s.bind(host)</p><p> s.bind(port)</p><p> s.listen(5)</p><p> except socket.error as msg:</p><p> print("Socket binding error: " + str(msg) + "\n" + "Retrynig....")</p><p></p><p> bind_socket()</p><p></p><p>#Establish connection with the client</p><p></p><p>def socket_accept():</p><p> conn, address = s.accept()</p><p> print("Connection has been established!" + " | IP: " + address[0] + " | Port: " + str(address))</p><p> send_command(conn)</p><p> conn.close()</p><p></p><p>#send commands to client</p><p>def send_command(conn):</p><p> while True:</p><p> terminal = input()</p><p> if terminal == "quit":</p><p> conn.close()</p><p> s.close()</p><p> sys.exit()</p><p></p><p> if len(str.encode(terminal)) > 0:</p><p> conn.send(str.encode(terminal))</p><p> client_response = str(conn.recv(1024), "utf-8")</p><p> print(client_response, end = "")</p><p></p><p>def main():</p><p> create_socket()</p><p> bind_socket()</p><p> socket_accept()</p><p></p><p>main()[/CODE]</p><p></p><p></p><p></p><p>error eka:- </p><p>/usr/bin/python3.11 /homej/Documents/server programming/1/server.py </p><p>Traceback (most recent call last):</p><p> File "/home/Documents/server programming/1/server.py", line 63, in <module></p><p> main()</p><p> File "/home/Documents/server programming/1/server.py", line 60, in main</p><p> bind_socket()</p><p> File "/home/Documents/server programming/1/server.py", line 28, in bind_socket</p><p> s.bind(host)</p><p>TypeError: bind(): AF_INET address must be tuple, not str</p><p>Binding the Port: 9999</p><p></p><p>Process finished with exit code 1</p><p></p><p></p><p>Chat GPT generate karala hadawapu eka:-</p><p></p><p>[CODE=python]import socket</p><p>import sys</p><p></p><p># Creating socket (Connect with 2 pcs)</p><p>def create_socket():</p><p> try:</p><p> global host</p><p> global port</p><p> global s</p><p></p><p> host = "127.0.0.1"</p><p> port = 9999</p><p> s = socket.socket()</p><p></p><p> except socket.error as msg:</p><p> print("Socket creation error: " + str(msg))</p><p></p><p># Binding the socket and listening for connections</p><p>def bind_socket():</p><p> try:</p><p> global host</p><p> global port</p><p> global s</p><p></p><p> print("Binding the Port: " + str(port))</p><p></p><p> # Use a tuple for the address</p><p> s.bind((host, port))</p><p> s.listen(5)</p><p> except socket.error as msg:</p><p> print("Socket binding error: " + str(msg) + "\n" + "Retrying....")</p><p> bind_socket()</p><p></p><p># Establish connection with the client</p><p>def socket_accept():</p><p> conn, address = s.accept()</p><p> print("Connection has been established! | IP: " + address[0] + " | Port: " + str(address[1]))</p><p> send_command(conn)</p><p> conn.close()</p><p></p><p># Send commands to client</p><p>def send_command(conn):</p><p> while True:</p><p> terminal = input()</p><p> if terminal == "quit":</p><p> conn.close()</p><p> s.close()</p><p> sys.exit()</p><p></p><p> if len(str.encode(terminal)) > 0:</p><p> conn.send(str.encode(terminal))</p><p> client_response = str(conn.recv(1024), "utf-8")</p><p> print(client_response, end="")</p><p></p><p>def main():</p><p> create_socket()</p><p> bind_socket()</p><p> socket_accept()</p><p></p><p>if __name__ == "__main__":</p><p> main()</p><p>[/CODE]</p><p></p><p>GPT sats:-</p><p>Here are the key changes I made:</p><p></p><ol> <li data-xf-list-type="ol">In the bind_socket() function, I updated s.bind(host) to s.bind((host, port)) to provide a tuple as the address argument to the bind() method.</li> <li data-xf-list-type="ol">In the socket_accept() function, I updated the IP address and port printing to use address[1] instead of address for the port.</li> <li data-xf-list-type="ol">I added a guard condition if __name__ == "__main__": around the main() function call. This ensures that the main() function is executed only if the script is run directly (not imported as a module).</li> </ol><p>With these changes, your code should work as intended, binding the socket to the specified address and port and allowing you to send commands to the connected client.</p></blockquote><p></p>
[QUOTE="hecker_thama, post: 29100768, member: 582558"] Me mage code eka [CODE=python]import socket import sys #creating socket(Connect with 2 pcs) def create_socket(): try: global host global port global s host = "127.0.0.1" port = 9999 s = socket.socket() except socket.error as msg: print("Socket creation error: " + str(msg)) #binding the socket and listening for connections def bind_socket(): try: global host global port global s print("Binding the Port: " + str(port)) s.bind(host) s.bind(port) s.listen(5) except socket.error as msg: print("Socket binding error: " + str(msg) + "\n" + "Retrynig....") bind_socket() #Establish connection with the client def socket_accept(): conn, address = s.accept() print("Connection has been established!" + " | IP: " + address[0] + " | Port: " + str(address)) send_command(conn) conn.close() #send commands to client def send_command(conn): while True: terminal = input() if terminal == "quit": conn.close() s.close() sys.exit() if len(str.encode(terminal)) > 0: conn.send(str.encode(terminal)) client_response = str(conn.recv(1024), "utf-8") print(client_response, end = "") def main(): create_socket() bind_socket() socket_accept() main()[/CODE] error eka:- /usr/bin/python3.11 /homej/Documents/server programming/1/server.py Traceback (most recent call last): File "/home/Documents/server programming/1/server.py", line 63, in <module> main() File "/home/Documents/server programming/1/server.py", line 60, in main bind_socket() File "/home/Documents/server programming/1/server.py", line 28, in bind_socket s.bind(host) TypeError: bind(): AF_INET address must be tuple, not str Binding the Port: 9999 Process finished with exit code 1 Chat GPT generate karala hadawapu eka:- [CODE=python]import socket import sys # Creating socket (Connect with 2 pcs) def create_socket(): try: global host global port global s host = "127.0.0.1" port = 9999 s = socket.socket() except socket.error as msg: print("Socket creation error: " + str(msg)) # Binding the socket and listening for connections def bind_socket(): try: global host global port global s print("Binding the Port: " + str(port)) # Use a tuple for the address s.bind((host, port)) s.listen(5) except socket.error as msg: print("Socket binding error: " + str(msg) + "\n" + "Retrying....") bind_socket() # Establish connection with the client def socket_accept(): conn, address = s.accept() print("Connection has been established! | IP: " + address[0] + " | Port: " + str(address[1])) send_command(conn) conn.close() # Send commands to client def send_command(conn): while True: terminal = input() if terminal == "quit": conn.close() s.close() sys.exit() if len(str.encode(terminal)) > 0: conn.send(str.encode(terminal)) client_response = str(conn.recv(1024), "utf-8") print(client_response, end="") def main(): create_socket() bind_socket() socket_accept() if __name__ == "__main__": main() [/CODE] GPT sats:- Here are the key changes I made: [LIST=1] [*]In the bind_socket() function, I updated s.bind(host) to s.bind((host, port)) to provide a tuple as the address argument to the bind() method. [*]In the socket_accept() function, I updated the IP address and port printing to use address[1] instead of address for the port. [*]I added a guard condition if __name__ == "__main__": around the main() function call. This ensures that the main() function is executed only if the script is run directly (not imported as a module). [/LIST] With these changes, your code should work as intended, binding the socket to the specified address and port and allowing you to send commands to the connected client. [/QUOTE]
Insert quotes…
Verification
Winadiyakata thappara keeyak tibeda?
Post reply
Top
Bottom