How to use client socket as a server socket python

MKM

I like to have one port that first use for connect to another server and after that this port use to be a server and another clients connect to it.

I used python socket for client now I want to use it for server socket.
my code :

#!/usr/bin/python           # This is server.py file
import socket               # Import socket module
s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12349
portt = 12341               # Reserve a port for your service.
s.bind((host, portt))       # Bind to the port

s.connect((host, port))
s.listen(5)                 # Now wait for client connection.
c, addr = s.accept()        # Establish connection with client.
print c
print 'Got connection from', addr
print s.recv(1024)
s.close

and the output is

Traceback (most recent call last):
  File "client.py", line 12, in <module>
    s.listen(5)                 # Now wait for client connection.
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 22] Invalid argument

How can I do that. thank you for your answers!

bosnjak

Not sure what you are trying to do here. Seems to me that you are mixing client and server code in the same app.

For reference, you can create a simple echo server like this:

import socket

HOST = ''                
PORT = 12349     
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(5)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
    data = conn.recv(1024)
    if not data: break
    conn.sendall(data)
conn.close()

And a simple echo client like this:

import socket

HOST = 'localhost'
PORT = 12349              
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use socket with a Python client and a C++ server

From Dev

Python how to make a socket server use a command built into the client

From Dev

How to connect python socket client side to server socket with url?

From Dev

make client socket wait for server socket with Python

From Dev

How to make client socket wait for server socket

From Dev

Python socket server/client programming

From Dev

UDP Client/Server Socket in Python

From Dev

Python socket server/client not looping

From Dev

Socket Java client - Python Server

From Dev

How to make a java client use a socket to connect to a server not on my computer?

From Dev

How to secure messages travelling between a socket client and server in Python 3?

From Dev

Python 2.7.8: Socket - Client Server Data loss in TCP socket

From Dev

Python Client to nodeJS Server with Socket.IO

From Dev

Socket Programming with Python server and Android client

From Dev

client to server, socket in python many to one relationship

From Dev

Python simple socket client/server using asyncio

From Dev

Socket Programming with Python server and Android client

From Dev

Python socket server - continue to receive client data

From Dev

RAW client-server socket python

From Dev

A simple socket server and client program using python

From Dev

How to disconnect a client from NIO server socket

From Dev

How to make the client wait for the server socket connection?

From Dev

How to disconnect a client from NIO server socket

From Dev

How to make the client wait for the server socket connection?

From Dev

Android Socket (Client - Server)

From Dev

Java Socket client & server

From Dev

Server/Client Socket connection

From Dev

Client - Server socket

From Dev

How to use callback on a socket.io CLIENT?

Related Related

  1. 1

    How to use socket with a Python client and a C++ server

  2. 2

    Python how to make a socket server use a command built into the client

  3. 3

    How to connect python socket client side to server socket with url?

  4. 4

    make client socket wait for server socket with Python

  5. 5

    How to make client socket wait for server socket

  6. 6

    Python socket server/client programming

  7. 7

    UDP Client/Server Socket in Python

  8. 8

    Python socket server/client not looping

  9. 9

    Socket Java client - Python Server

  10. 10

    How to make a java client use a socket to connect to a server not on my computer?

  11. 11

    How to secure messages travelling between a socket client and server in Python 3?

  12. 12

    Python 2.7.8: Socket - Client Server Data loss in TCP socket

  13. 13

    Python Client to nodeJS Server with Socket.IO

  14. 14

    Socket Programming with Python server and Android client

  15. 15

    client to server, socket in python many to one relationship

  16. 16

    Python simple socket client/server using asyncio

  17. 17

    Socket Programming with Python server and Android client

  18. 18

    Python socket server - continue to receive client data

  19. 19

    RAW client-server socket python

  20. 20

    A simple socket server and client program using python

  21. 21

    How to disconnect a client from NIO server socket

  22. 22

    How to make the client wait for the server socket connection?

  23. 23

    How to disconnect a client from NIO server socket

  24. 24

    How to make the client wait for the server socket connection?

  25. 25

    Android Socket (Client - Server)

  26. 26

    Java Socket client & server

  27. 27

    Server/Client Socket connection

  28. 28

    Client - Server socket

  29. 29

    How to use callback on a socket.io CLIENT?

HotTag

Archive