473,772 Members | 3,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TCP servers in Python - two processes want to use same port

I am writing programs that will run as TCP servers. Briefly, I want to
set up a TCP server on a port in such a way that if another server is
already sitting on that port (both servers are Python programs I will be
writing), the old one is booted off (and its process ended).

My master process is a test harness, which tests a script which as part
of its functionality sends http GET messages. My test harness sets up
an http server as a child process, by creating an instance of
popen2.Popen3() .

The child process is a very simple program using SimpleHTTPServe r. The
problem is when I run my test harness, break it with Ctrl-C then try to
run it again. The http server complains that it cannot bind to the socket,
as the address is already in use.

My production system will use several http servers (I'm using http GET
messages as a simple form of inter-process communication), and I don't
want to have a situation where a program runs one time (because it's
asking to bind to a port that isn't already in use), but the next time,
it doesn't run because the port is being used.

Now of course I could use files to describe what ports are currently in
use and what process ids are using them, and kill the process before
trying to re-use the port, but that solution strikes me as inelegant.
Is there a better way? For example, is there a way of writing a program
that will force binding to a port, and if an existing process has that
port will kick it off (and possibly destroy it)?

--
Pif Paf
Jul 18 '05 #1
4 3488
Pif Paf wrote:

The child process is a very simple program using SimpleHTTPServe r. The
problem is when I run my test harness, break it with Ctrl-C then try to
run it again. The http server complains that it cannot bind to the socket,
as the address is already in use.


I believe you should be looking at SO_REUSE_ADDR as a means of rebinding
to a socket that was just used recently and may still be active at the
OS level, even if the app that used it has exited.

As for the specific question about ways to kill the app that is listening
on a socket already: I think if you don't know which app it is, there
isn't a clean way to find out. If you do know, though, as your test
harness probably does, can't it just "kill -9" the thing?

-Peter
Jul 18 '05 #2
Pif Paf wrote:
I am writing programs that will run as TCP servers. Briefly, I want to
set up a TCP server on a port in such a way that if another server is
already sitting on that port (both servers are Python programs I will be
writing), the old one is booted off (and its process ended).

My master process is a test harness, which tests a script which as part
of its functionality sends http GET messages. My test harness sets up
an http server as a child process, by creating an instance of
popen2.Popen3() .

The child process is a very simple program using SimpleHTTPServe r. The
problem is when I run my test harness, break it with Ctrl-C then try to
run it again. The http server complains that it cannot bind to the socket,
as the address is already in use.

My production system will use several http servers (I'm using http GET
messages as a simple form of inter-process communication), and I don't
want to have a situation where a program runs one time (because it's
asking to bind to a port that isn't already in use), but the next time,
it doesn't run because the port is being used.

Now of course I could use files to describe what ports are currently in
use and what process ids are using them, and kill the process before
trying to re-use the port, but that solution strikes me as inelegant.
Is there a better way? For example, is there a way of writing a program
that will force binding to a port, and if an existing process has that
port will kick it off (and possibly destroy it)?

--
Pif Paf

I think you might have better luck in a TCP networking group, as this
isn't really a python question (unless python has some nify TCP api).
you might check into Twisted to see what's there for nifty apis.

If you're talking unix, you might want to spawn both servers as children
of the same parent. after launching both servers, the parent just waits
as a manager process. when the second child is unable to acquire the
port, it can signal the parent. the parent can send a stop/kill signal
to the child.

signaling a process requires the process id (pid). the children don't
know each others process ids. but they have their parent's process id.
The parent gets the process ids of all children.

not sure how that would apply in windows. don't think windows even has
signals. if so, you would have to substitute some other form of
interprocess communications.


Jul 18 '05 #3
Peter Hansen <pe***@engcorp. com> wrote in message news:<40******* ********@engcor p.com>...
Pif Paf wrote:

The child process is a very simple program using SimpleHTTPServe r. The
problem is when I run my test harness, break it with Ctrl-C then try to
run it again. The http server complains that it cannot bind to the socket,
as the address is already in use.
I believe you should be looking at SO_REUSE_ADDR as a means of rebinding
to a socket that was just used recently and may still be active at the
OS level, even if the app that used it has exited.


I will give that a go.
As for the specific question about ways to kill the app that is listening
on a socket already: I think if you don't know which app it is, there
isn't a clean way to find out.
Clearly the operating system knows, so there should be a way to find out.
If you do know, though, as your test
harness probably does, can't it just "kill -9" the thing?


The test harness typically does know the process. However, if the test
harness stops without doing proper cleanup (e.g. deleting a child processes
it has created), then when it is restarted there will be "debris" from
the last run that is stopping it from running properly. This is not,
perhaps, a big issue when it's just the test harness, but it is a major
issue on the production system. If the production system goes down for
any reason, I want to be able to bring it back up again by typing one
command from the command line. This will run the master process, which in
turn will restart other processes, and it is not acceptable for an old
process to be clogging up a socket.

--
Pif Paf
Jul 18 '05 #4
Pif Paf wrote:
As for the specific question about ways to kill the app that is listening
on a socket already: I think if you don't know which app it is, there
isn't a clean way to find out.

Clearly the operating system knows, so there should be a way to find out.


The OS knows a lot more than your user space applications, and that
is a good thing, for security reasons.

--Irmen
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2834
by: Paul Winkler | last post by:
This is driving me up the wall... any help would be MUCH appreciated. I have a module that I've whittled down into a 65-line script in an attempt to isolate the cause of the problem. (Real domain names have been removed in everything below.) SYNOPSIS: I have 2 target servers, at https://A.com and https://B.com. I have 2 clients, wget and my python script.
8
5267
by: Rahul | last post by:
Hi. I am part of a group in my univ where we organize a programming contest. In this contest we have a UDP based server. The server simulates a game and each contestant is to develop a team of virtual players. Each team is composed of 75 similar bots...i.e. governed by the same logic. Thus the contestant submits a single copy of the client and we instantiate the same program 75 times at the same time. The problem is that while executables...
0
6448
by: Danny Jensen | last post by:
I need to test if certain processes on a unix box were running. I wanted to use whatsup gold to do the testing. First I needed to go to the whatsup configure>monitors & services menu to add this tcp/ip port 1555 service with the folowing lines: Send=psef /dj/myco/rf.monitor\r\n Expect=~1 the psef above is a command that the unix server executes. The unix box communicates back a 1 if the test is successful and a 0 if it is
9
5068
by: john smile | last post by:
Hi All, I want to lock 2 tables on 2 servers using TABLOCKX hint. These tables function as semaphores in my application. It means when the tables are locked then other users will not be able to access them and automatically they can not continue their works. I have tried using the following code, but it does not work. I always got the error :
12
6833
by: Jay | last post by:
ok, i thought for 2 seconds i might have created a Keylogger in python but i still have one major think stopping me... PYTHON. when i run the program i have python create a file named keylog2.log and it then logs all keys pressed/typed in the python IDE into that file. All i want to know now is how do i hide or background python so that it will log all the keys pressed outside of Python. feel free to play around with my program... but...
9
2035
by: corey.coughlin | last post by:
Alright, so I've been following some of the arguments about enhancing parallelism in python, and I've kind of been struck by how hard things still are. It seems like what we really need is a more pythonic approach. One thing I've been seeing suggested a lot lately is that running jobs in separate processes, to make it easy to use the latest multiprocessor machines. Makes a lot of sense to me, those processors are going to be more and...
14
19801
by: Rochester | last post by:
Hi, I just found out that the general open file mechanism doesn't work for named pipes (fifo). Say I wrote something like this and it simply hangs python: #!/usr/bin/python import os
5
1641
by: walterbyrd | last post by:
I don't know much php either, but running a php app seems straight forward enough. Python seems to always use some sort of development environment vs production environment scheme. For development, you are supposed to run a local browser and load 127.0.0.1:5000 - or something like that. Then to run the same thing in a development environment, I have to configure some files, or touch all the files, restart the web-server, or something....
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10104
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.